Overwriting a method of a super class in the subclass ???

Hi,
can somebody tell me whether it's possible to add a new implementation of a method in the sub class which is inherited from a super class?
I want to model my program in that way:
1. I define a super class with some implemented methods.
2. This class should be inherited in a sub class. One method should be used as it was implemented in the super class but another method should be overwritten in the subclass.
I know this concept from Java but I couldn't find a way how to do it in ABAP
Many thanks for any help!
Best regards,
Birgit

hi,
yeas you can do it,
Subclass can re-implement  the inherited public and protected methods from superclass.Class C1 contains method METH1(public) and METH2(protected), both of which are modified and re-implemented in  its subclass C2.also you can have ur own methods in subclass.Objects are created out of both classes and the method METH1 for both objects are called.
Output of the program demonstrates different behaviour for method METH1 of class C1 and C2.
This demonstrates the theme.
REPORT YSUBDEL.
CLASS C1 DEFINITION.
  PUBLIC SECTION.
   METHODS : METH1.
  PROTECTED SECTION.
   METHODS METH2.
  ENDCLASS.
CLASS C1 IMPLEMENTATION .
  METHOD : METH1.
   WRITE:/5 'I am meth1 in class C1'.
   CALL METHOD METH2.
  ENDMETHOD.
  METHOD : METH2.
   WRITE:/5 ' I am meth2 in class C1 '.
  ENDMETHOD.
ENDCLASS.
CLASS C2 DEFINITION INHERITING FROM C1.
PUBLIC SECTION.
  METHODS : METH1 redefinition,
  meth3.
PROTECTED SECTION.
  METHODS : METH2 redefinition.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
METHOD METH1.
   WRITE:/5 'I am meth1 in class C2'.
   call method meth2.
endmethod.
  METHOD : METH2.
  WRITE:/5 ' I am meth2 in class C2 '.
ENDMETHOD.
METHOD : METH3.
  WRITE:/5 ' I am own method of class C2'.
ENDMETHOD.
endclass.
START-OF-SELECTION.
  DATA : OREF1 TYPE REF TO C1 ,
         OREF2 TYPE REF TO C2.
  CREATE OBJECT :  OREF1 , OREF2.
  CALL METHOD : OREF1->METH1 ,
                OREF2->METH1.
hope it helps,
regards

Similar Messages

  • Calling a method from a super class

    Hello, I'm trying to write a program that will call a method from a super class. This program is the test program, so should i include extends in the class declaration? Also, what code is needed for the call? Just to make things clear the program includes three different types of object classes and one abstract superclass and the test program which is what im having problems with. I try to use the test program to calculate somthing for each of them using the abstract method in the superclass, but its overridden for each of the three object classes. Now to call this function what syntax should I include? the function returns a double. Thanks.

    Well, this sort of depends on how the methods are overridden.
    public class SuperFoo {
      public void foo() {
         //do something;
      public void bar(){
         //do something
    public class SubFoo extends SuperFoo {
       public void foo() {
          //do something different that overrides foo()
       public void baz() {
          bar(); //calls superclass method
          foo(); //calls method in this (sub) class
          super.foo(); //calls method in superclass
    }However, if you have a superclass with an abstract method, then all the subclasses implement that same method with a relevant implementation. Since the parent method is abstract, you can't make a call to it (it contains no implementation, right?).

  • Hiding of super class members in subclass

    I want to hide super class members in subclass. like this
    class super{
    public int method(){
    class sub extends super{
    public string method(){
    give any solution for this criteria, I cann't make methods as private

    In java there is no way to override
    the methodsThat's incorrect. You can override methods in Java.
    However, you cannot do what the OP is trying to do.

  • How to override a method in an inner class of the super class

    I have a rather horribly written class, which I need to adapt. I could simply do this if I could override a method in one of it's inner classes. My plan then was to extend the original class, and override the method in question. But here I am struggling.
    The code below is representative of my situation.
    public class ClassA
       ValueChecks vc = null;
       /** Creates a new instance of Main */
       public ClassA()
          System.out.println("ClassA Constructor");
          vc = new ValueChecks();
          vc.checkMaximum();
         // I want this to call the overridden method, but it does not, it seems to call
         // the method in this class. probably because vc belongs to this class
          this.vc.checkMinimum();
          this.myMethod();
       protected void myMethod()
          System.out.println("myMethod(SUPER)");
       protected class ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUPER)");
             return true;
          protected boolean checkMaximum()
             return false;
    }I have extended ClassA, call it ClassASub, and it is this Class which I instantiate. The constructor in ClassASub obviously calls the constructor in ClassA. I want to override the checkMinimum() method in ValueChecks, but the above code always calls the method in ClassA. The ClassASub code looks like this
    public class ClassASub extends ClassA
       public ClassAInner cias;
       /** Creates a new instance of Main */
       public ClassASub()
          System.out.println("ClassASub Constructor");
       protected void myMethod()
          System.out.println("myMethod(SUB)");
       protected class ValueChecks extends ClassA.ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUB)");
             return true;
    }The method myMethod seems to be suitably overridden, but I cannot override the checkMinimum() method.
    I think this is a stupid problem to do with how the ValueChecks class is instantiated. I think I need to create an instance of ValueChecks in ClassASub, and pass a reference to it into ClassA. But this will upset the way ClassA works. Could somebody enlighten me please.

    vc = new ValueChecks();vc is a ValueChecks object. No matter whether you subclass ValueChecks or not, vc is always of this type, per this line of code.
    // I want this to call the overridden method, but it does not, it seems to > call
    // the method in this class. probably because vc belongs to this class
    this.vc.checkMinimum();No, it's because again vc references a ValueChecks object, because it was created as such.
    And when I say ValueChecks, I mean the original class, not the one you tried to create by the same name, attempting to override the original.

  • Is there a way to refer to/call the method of a super class' super class?

    Is there any way to essentially do what this pseudo code does?
    public void someMethod()
    return super.super.someMethod();
    }

    Imagine you had a class called C that extends class B, and class B extends class A. They all declare an instance variable called i. In the case of accessing an instance variable you could do this in class C:
    ((A)this).iThis is because super means the same as a cast of this.
    This is not true for for method invocation though. The cast to type A does not change the method that is invoked, because the instance method to be invoked is chosen according to the run-time class of the object referred to by this. A cast does not change the class of an object; it only checks that the class is compatible with the specified type.
    This is all in the JLS.

  • Instantiation of similar object over a super class deciding the sub class

    Hello all
    First, sorry if I'm duplicating an already answered question. I didn't searched very deep.
    Initial position:
    I have 2 Object (A1 and A2) which share the most (about 90%) of their instance variables an the associated methods. The values of the instance variables are retrieved in the real implementation from a stream. Depending of the data of the stream, I have to instantiate either a A1 or A2 object.
    A test implementation (using an int in case of the stream):
    The super class A:
    package aaa;
    public class A
      protected int version = -1;
      protected String name = null;
      protected AE ae = null;
      protected A()
      protected A(int v)
        // Pseudo code
        if (v > 7)
          return;
        if (v % 2 == 1)
          this.version = 1;
        else
          this.version = 2;
      public final int getVersion()
        return this.version;
      public final String getName()
        return this.name;
      public final AE getAE()
        return this.ae;
    }The first sub class A1:
    package aaa;
    public final class A1 extends A
      protected A1(int v)
        this.version = v;
        this.name = "A" + v;
        this.ae = new AE(v);
    }The second subclass A2:
    package aaa;
    import java.util.Date;
    public final class A2 extends A
      private long time = -1;
      protected A2(int v)
        this.version = v;
        this.name = "A" + v;
        this.time = new Date().getTime();
        this.ae = new AE(v);
      public final long getTime()
        return this.time;
    }Another class AE:
    package aaa;
    public class AE
      protected int type = -1;
      protected AE(int v)
        // Pseudo code
        if (v % 2 == 1)
          this.type = 0;
        else
          this.type = 3;
      public final int getType()
        return this.type;
    }To get a specific object, I use this class:
    package aaa;
    public final class AFactory
      public AFactory()
      public final Object createA(int p)
        A a = new A(p);
        int v = a.getVersion();
        switch (v)
        case 1:
          return new A1(v);
        case 2:
          return new A2(v);
        default:
          return null;
    }And at least, a class using this objects:
    import aaa.*;
    public final class R
      public static void main(String[] args)
        AFactory f = new AFactory();
        Object o = null;
        for (int i = 0; i < 10; i++)
          System.out.println("===== Current Number is " + i + " =====");
          o = f.createA(i);
          if (o instanceof aaa.A)
            A a = (A) o;
            System.out.println("Class   : " + a.getClass().getName());
            System.out.println("Version : " + a.getVersion());
            System.out.println("Name    : " + a.getName());
            System.out.println("AE-Type : " + a.getAE().getType());
          if (o instanceof aaa.A2)
            A2 a = (A2) o;
            System.out.println("Time    : " + a.getTime());
          System.out.println();
    Questions:
    What would be a better way to encapsulate the logic into their respective objects ? Is there a way to let the super class A itself identify the type of the object and then extend from A to either A1 or A2 ?
    Thanks in advance
    Andreas

    Hello jduprez
    First, I would thank you very much for taking the time reviewing my problem.
    Just for the record: have you considered regular serialization? If you control the software at both ends of the stream, you could rely on standard serialization mechanism to marshall the objects and unmarshall them automatically.In my case, I can't control the other site of the stream. At the end, the data comes from a FileInputStream and there aren't objects on the other site, only pur binary data.
    - It seems you have one such factory class. Then you already have encapsulated the "determine class" logic, you don't need to add such logic in superclass A.I thought from an OO view, that the super class A is responsible of doing that, but that's where the problem starts. So at the end, it's better doing it in the factory class.
    - A itself encapsulates the logic to load its own values from the stream.
    - A1 and A2 can encapsulate the logic to load their own specific value from a stream (after the superclass' code has loaded the superclass' attributes values).
    My advise would be along the lines of:
    public class A {
    .... // member variables
    public void load(InputStream is) {
    ... // assign values to A's member variables
    // from what is read from the stream.
    public class A1 extends A {
    ... // A1-specific member variables
    public void load(InputStream is) {
    super.load(is);
    // now read A1-specific values
    public class AFactory {
    public A createA(InputStream is) {
    A instance;
    switch (is.readFirstByte()) {
    case A1_ID:
    a = new A1();
    break;
    case A2_ID:
    a = new A2();
    break;
    a.load(is);
    }The example above assumes you have control over the layout of the data in the stream (here for a given A instance, the attributes defined in A are read first, then come the subclass-specific attributes.
    The outcome is: you don't have to create a new A( ) to later create another instance, of a subclass.I like the idea. In the AFactory, is the "A instance;" read as "A a;" ?
    Is there a way to let the super class A itself identify the type of the object and then extend from A to either A1 or A2 ?Note I initially read this question as "can an instance of a class mutate into another class", to which the answer is no (an object has one single, immutable class; it is an instance of this class, and of any superclass and superinterface, but won't change its class at runtime).Yes, I have been thinking about a way for mutating into a subclass to keep the already initialized values from the A class without copying or parsing again. But preventing an instance of an A class will be my way. So, in this aspect, Java didn't changed in the last 10 years... It's a long time ago I've used Java for some real projects.
    You can, however, create an instance of another class, that copies the values off a priori A instance. Your example code was one way, another way could be to have a "copy constructor":
    public class A {
    public A(A model) {
    this.att1 = model.att1;
    this.att2 = model.att2;
    public class A1 {
    public A1(A model) {
    super(model);
    ... // do whatever A1-specific business
    )Still, I prefer my former solution less disturbing: I find the temporary A instance redundant and awkward.Nice to know. I prefer the first solution too.
    Thank you again for the help and advices. My mind is searching sometimes for strange solutions, where the real is so close ;-)
    Andreas

  • Using main class's methods from a different class in the same file

    Hi guys. Migrating from C++, hit a few snags. Hope someone can furnish a quick word of advice here.
    1. The filename is test.java, so test is the main class. This code and the topic title speak for themselves:
    class SomeClass
         public void SomeMethod()
              System.out.println(test.SomeOperation());
    public class test
         public static void main(String args[])
              SomeClass someObject = new SomeClass();
              someObject.SomeMethod();
         public static String SomeOperation()
              return "SomeThing";
    }The code works fine. What I want to know is, is there some way to use test.SomeOperation() from SomeClass without the test.?
    2. No sense opening a second topic for this, so second question: Similarly, is there a good way to refer to System.out.println without the System.out.? Like the using keyword in C++.
    Thanks.

    pfiinit wrote:
    The code works fine. What I want to know is, is there some way to use test.SomeOperation() from SomeClass without the test.?Yes you can by using a static import, but I don't recommend it. SomeOperation is a static method of the test class, and it's best to call it that way so you know exactly what your code is doing here.
    2. No sense opening a second topic for this, so second question: Similarly, is there a good way to refer to System.out.println without the System.out.? Like the using keyword in C++.Again, you could use static imports, but again, I don't recommend it. Myself, I use Eclipse and set up its template so that when I type sop it automatically spits out System.out.println(). Most decent IDE's have this capability.
    Also, you may wish to look up Java naming conventions, since if you abide by them, it will make it easier for others to understand your code.
    Much luck and welcome to this forum!

  • Using super class constructor in subclass constructor

    Hi all. How can I use Person() in my Client()?
    something like this:
    public class Client extends Person{
          private float balance;
          Client(String firstName,String midleName,String lastName,Contacts contacts,Adress adress,float balance){
               this= Person(firstName,midleName,lastName,contacts,adress);
               this.balance=balance;
          }

    public class Client extends Person{
      private float balance; // float?!? Are you sure.... ?
      Client(String firstName,String midleName,String lastName,Contacts contacts,Adress adress,float balance){
        super(firstName,midleName,lastName,contacts,adress);
        this.balance=balance;
    }

  • How to call a method of a base class if the base class is abstract...

    This is my sample code>>>>
    abstract class b {
         public void display() {
              System.out.println("I am in Base Class");
    class test extends b {
         public static void main(String[] args) {
              test obj = new test();
              obj.display();
         public void display() {
              System.out.println("I am in Derived Class");
    I want to call base class version of display with derived class object.........is it possible....

    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    * headdesk *
    Enough with the friggin' zombie threads already!

  • Overiding super class method to an abstract  method

    public class Super
    public void doSomethingUseful()
    public abstract class Sub extends Super
    public abstract void doSomethingUseful();
    What is the OO principle behind this?
    When do we need to override a super class method in subclass as an abstract?

    Lets first look at a simple design pattern called "Template Method".
    public abstract class Library
    private void collectBooks()
    // collect books here
    private void putBookInShelf()
    // put books in shelf here
    // abstract method sortBooks()
    public abstract void sortBooks();
    public void processBooks()
    collectBooks();
    sortBooks();
    putBooksInShelf();
    this class is an abstract class giving an abstract method called "sortBooks()", what is it useful for? We can make a subclass and implement sortBooks() to sort the books as we want (title wise, author wise, date wise, publisher wise) and then simply call processBooks() to process them.
    One Sub class may look like:
    public class MyLibrary extends Library
    public void sortBooks()
    // sort books by title b/c I like them sorted out by title
    Another sub-class may look like
    public class HisLibrary extends Library
    public void sortBooks()
    // sort books by Author, b/c he likes his books sorted out by author
    Now client will say:
    public static void main(String str[])
    MyLibrary mylib=new MyLibrary();
    mylib.processBooks(); // books will be processed by sorting them title wise
    HisLibrary hislib=new HisLibrary();
    hislib.processBooks(); // books will be processed by sorting them author wise
    So in Library class, method "sortBooks()" was a template method allowing subclasses to sort the books as they want while all other functionality was implemented by Library class itself.
    Now if we go back to your example, a method which is concrete in super class that you converted into an abstract method in sub class ( doSomethingUseful() ) is now a template method, which alows the sub classes of this subclass to do something useful what they think is useful or in other words you are allowing subclasses of this subclass to implement this template method as they want by using their own algorithm.
    Now why whould you do that? answer is that you don't have access to the code of super class, otherwise you must have made this method abstract in super class in the first place.
    Note that the code may not compile, I tried to come up with an exmple and did not pay attention to compiler demands.
    I think I cleared my point, It was tough to explain though.
    Good Luck.
    Khawar.

  • Inherited methods of super class not shown in outline

    Hello, is there a possibility to display the methods and variables from the super class in the outline of the inheriting class in AiE?

    Hi Christian,
    We had discussed this, when we implemented the outline and quick outline some years, ago. Finally, we sticked to the approach that is used by other plug-ins in Eclipse (like JDT).
    There Outline View shows only the content of the current editor. This helps to keep the content of the view stable when you select an element for navigation (because you won't be able to switch the editor by selecting an element ).
    Inherited members are usually part of other editors, unless you have redefined a method in your current class. Therefore, they are not shown in Outline View (unless they are redefinitions).
    The filters like "Hide Non-Public Members of classes" can only hide elements of the current editor.
    Inherited members can be shown in the Quick Outline, because the interaction pattern is different there: If you navigate to an element the pop-up is closed. Therefore, a content change of the Quick-Outline does not hurt.
    Michael

  • Regarding Super classes

    Hi,
    I have created one class in SE24. This class will be used as a super class for other classes.
    When subclasses are derived from this superclass, i want to make sure that some of the methods of superclasses are redefined by the subclasse compulsarily.
    So i want to force the subclasses to redefine complusarily some of the methods of its super class.
    Is this feasible. If so please let me know the corresponding approach.
    Thanks in advance !
    Pramod

    Hi,
    Check this out this will help you.
    Inheritance is the concept of passing the behavior of a class to another class.
    1.You can use an existing class to derive a new class.
    2.Derived class inherits the data and methods of a super class.
    3.However they can overwrite the methods existing methods and also add new once.
    4.Inheritance is to inherit the attributes and methods from a parent class.
    Inheritance:
    Inheritance is the process by which object of one class acquire the properties of another class.
    Advantage of this property is reusability.
    This means we can add additional features to an existing class with out modifying it.
    Go to SE38.
    Provide the program name.
    Provide the properties.
    Save it.
    Provide the logic for inheritance.
    *& Report  ZLOCALCLASS_VARIABLES                      *
    *&----------------------------------------------------*REPORT  ZLOCALCLASS_VARIABLES.
    *OOPS INHERITANCE
    *SUPER CLASS FUNCTIONALITY
    *DEFINE THE CLASS.
    CLASS CL_LC DEFINITION.
    PUBLIC SECTION.
    DATA: A TYPE I,
          B TYPE I,
          C TYPE I.
    METHODS: DISPLAY,
             MM1.
    CLASS-METHODS: MM2.
    ENDCLASS.
    *CLASS IMPLEMENTATION
    CLASS CL_LC IMPLEMENTATION.
    METHOD DISPLAY.
    WRITE:/ 'THIS IS SUPER CLASS' COLOR 7.
    ENDMETHOD.
    METHOD MM1.
    WRITE:/ 'THIS IS MM1 METHOD IN SUPER CLASS'.
    ENDMETHOD.
    METHOD MM2.
    WRITE:/ 'THIS IS THE STATIC METHOD' COLOR 2.
    WRITE:/ 'THIS IS MM2 METHOD IN SUPER CLASS' COLOR 2.
    ENDMETHOD.
    ENDCLASS.
    *SUB CLASS FUNCTIONALITY
    *CREATE THE CLASS.
    *INHERITING THE SUPER CLASS.
    CLASS CL_SUB DEFINITION INHERITING FROM CL_LC. "HOW WE CAN INHERIT
    PUBLIC SECTION.
    DATA: A1 TYPE I,
          B1 TYPE I,
          C1 TYPE I.
    METHODS: DISPLAY REDEFINITION,     "REDEFINE THE SUPER CLASS METHOD
             SUB.
    ENDCLASS.
    *CLASS IMPLEMENTATION.
    CLASS CL_SUB IMPLEMENTATION.
    METHOD DISPLAY.
    WRITE:/ 'THIS IS THE SUB CLASS OVERWRITE METHOD' COLOR 3.
    ENDMETHOD.
    METHOD SUB.
    WRITE:/ 'THIS IS THE SUB CLASS METHOD' COLOR 3.
    ENDMETHOD.
    ENDCLASS.
    *CREATE THE OBJECT FOR SUB CLASS.
    DATA: OBJ TYPE REF TO CL_SUB.
    START-OF-SELECTION.
    CREATE OBJECT OBJ.
    CALL METHOD OBJ->DISPLAY. "THIS IS SUB CLASS METHOD
    CALL METHOD OBJ->SUB.
    WRITE:/'THIS IS THE SUPER CLASS METHODS CALLED BY THE SUB CLASS OBJECT'COLOR 5.
    SKIP 1.
    CALL METHOD OBJ->MM1.     "THIS IS SUPER CLASS METHOD
    CALL METHOD OBJ->MM2.
    *CREATE THE OBJECT FOR SUPER CLASS.
    DATA: OBJ1 TYPE REF TO CL_LC.
    START-OF-SELECTION.
    CREATE OBJECT OBJ1.
    SKIP 3.
    WRITE:/ 'WE CAN CALL ONLY SUPER CLASS METHODS BY USING SUPER CLASS OBJECT' COLOR 5.
    CALL METHOD OBJ1->DISPLAY. "THIS IS SUPER CLASS METHOD
    CALL METHOD OBJ1->MM1.
    CALL METHOD OBJ1->MM2.
    This example will help you to solve your problem.
    For more detailed information GOTO -> SAPTECHNICAL ->Tutorials -> Object Oriented Programming.
    Regards Madhu.
    Code Formatted by: Alvaro Tejada Galindo on Jan 7, 2009 12:13 PM

  • Weird one..  i can't return a variable from the extended to the super class

    Hey everyone, i hope i'm not annoying you guys :)
    So today's problem is to return a variable (int) from a method of the extended class and print it ont the super class.
    I'm just testing the super class , if it works fine.
    So the extended class ( FileIO) just read the file txt and return the integer or string ( from the txt file)
    I already did a main method to that class and tested it, it works fine.
    So now the problem is to print the integer ( that the extended class gets from the txt. ) inside the Super class. I mean , is the same thing but now im testing the Super class , just have to do the same thing, a super class method calls the extended class method and receive the integer from the txt file.
    i think the problem is when i create the instance of the FileIO object , maybe its constructor ...i don't know.
    The name of the txt file is passed from the super class to the extended class, but i think the error is not from there.
    this.aero_le = new BufferedReader(new FileReader(super.ficheiroleitura_aero()));  //  super calls ficheiroleitura_aero()  and receive the name of the txt file ( e.g "temp.txt")  so i think that is correct.
    here's the code of the Super class public class Aeroporto {
         private String filereader_voo = "temporary.txt";
         private String filereader_aero = "temp.txt";
         private String siglaAero = "";
         public FileIO file;
         public Aeroporto(){};
         public Aeroporto(String filereader_voo, String filereader_aero) throws IOException{
              this.filereader_voo = filereader_voo;
              this.filereader_aero =filereader_aero;     
              file = new FileIO();
         public String siglaAero() {
              return siglaAero; }
         public String filereader_aero(){
              return filereader_aero;
    public int nrLines() throws IOException{   // it was supose to retunr the number of lines ( integer) from the txt file .
              return Integer.parseInt(file.lerLinhaN(1,1));
    // main() {
    Aeroporto a = new Aeroporto();
              int v = a.nrLines();
              System.out.print(v);
    // ***********************************************************+
    // Extended Class
    public class FileIO extends Aeroporto{
         private String ficheiroescrita;
         private PrintWriter vooescreve, aeroescreve ;
         private BufferedReader voo_le, aero_read;
         public FileIO(){}
         public FileIO(String filereader_voo, String filereader_aero, String ficheiroescrita) throws IOException {
              super(filereader_voo, filereader_aero);
              this.ficheiroescrita = ficheiroescrita;
              //If file does not exists , create one.
              try{
                   this.aero_read = new BufferedReader(new FileReader(super.filereader_aero()));
                   aero_read.close();
              catch(IOException ex){
                   this.aeroescreve = new PrintWriter(new FileWriter(ficheiroescrita));
                   aeroescreve.close();
    public String lerLinhaN(int line, int column) throws IOException{  // this method works fine , i already tested this class.
              this.aero_read = new BufferedReader(new FileReader(super.filereader_aero()));
              for(int i = 0; i != line-1; ++i) aero_read.readLine();
              String linha = aero_read.readLine();
              String [] words = linha.split(" ");
              return words[column-1];
    Maybe the error is that i use to test the Super class a default contructor on both classes... i don't know where the error is, i also did two small classes ( super and another that extends ) and get the string "Hello" from the super and print it inside the extended..and it works, that's why i think the error is when i call the extended class .. need help.
    thanks.

    Ok,
    This one might actually work... atleast it compiles.import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    public abstract class FileIO {
         public static boolean CreateOutputFileIfNotExists(
              String outputFilename //the name of the file to ensure exists.
         ) throws IOException
              final String functionName = "FileIO.CreateOutputFileIfNotExists";
              boolean retVal = false;
              //If the output file does does not exist then create it.
              //post condition: output file exists or an IOException has been thrown.
              BufferedReader infile = null;
              try{
                   infile = new BufferedReader(new FileReader(outputFilename));
                   retVal = true;
              } catch(IOException ex) {
                   PrintWriter outfile = null;
                   try {
                        outfile = new PrintWriter(new FileWriter(outputFilename));
                        retVal = true;
                   } catch(IOException ex2){
                        throw new IOException(functionName + ": cannot create output file " + outputFilename, ex2);
                   } finally {
                        outfile.close();
                        if (outfile.checkError()) {
                             throw new IOException(functionName + ": error on output stream " + outputFilename);
              } finally {
                   try {
                        infile.close();
                   } catch(IOException ex){
                        throw new IOException(functionName + ": cannot close output file " + outputFilename, ex);
              return(retVal);
         public static String readLine(
                   String  inputFilename //the name of the file to read.
              , int     lineNumber    //1 based number of the line to read from.
         ) throws IOException
              final String functionName = "FileIO.readLine";
              String outputLine = null;
              // reads the numbered "lineNumber" from "inputFilename".
              BufferedReader infile = null;
              try {
                   infile = new BufferedReader(new FileReader(new File(inputFilename)));
                   for(int i=1; i<lineNumber; ++i) infile.readLine();
                   outputLine = infile.readLine();
              } catch(IOException ex){
                   throw new IOException(functionName + ": cannot read input file " + inputFilename, ex);
              } finally {
                   try {
                        infile.close();
                   } catch(IOException ex){
                        throw new IOException(functionName + ": cannot close input file " + inputFilename, ex);
              return(outputLine);
         public static String readWord(
                   String inputFilename  //the name of the file to read.
              , int lineNumber        //1 based number of the line to read from.
              , int wordNumber        //0 based number of the word to read.
         ) throws IOException
              final String functionName = "FileIO.readWord";
              String outputWord = null;
              // reads the numbered space-seperated "wordNumber" from the numbered "lineNumber" of "inputFilename"
              try {
                   String[] words = FileIO.readLine(inputFilename, lineNumber).split(" ");
                   if (wordNumber>0 && wordNumber<words.length) outputWord = words[wordNumber-1];
              } catch(IOException ex){
                   throw new IOException(functionName + ": cannot read input file " + inputFilename, ex);
              return(outputWord);
    }Design notes... FileIO is a generic helper class... there is nothing specific to Airports, flights, or any other "domain" specific stuff in it... so it's re-usable... you can keep it and reuse it on other projects, or even share it with your friends.
    So... The airport class will just call the static methods on FileIO like this    ....
        int lineNumber=1;
        int wordNumber=1;
        String airportCode = FileIO.readWord(airportsFilename, lineNumber, wordNumber);
        ....How's that?
    corlettk: my now mandatory edit.

  • Calling a method in a different class?

    How do you call a method from a differnt class into the class you are working on?

    Class and method were just generic names. You should insert the names of your classes and methods.
    class OtherClass {
        public static void one() {
            System.out.println("Static method")l;
        public void two() {
            System.out.println("Non-static method");
    class MainClass {
        public static void main(String[] args) {
            OtherClass.one();
            OtherClass oc = new OtherClass();
            oc.two();
    }

  • Is it possible to override super class constructor?

    Is it possible to override super class constructor form subclass?

    However, you can achieve do something that looks similar to overriding.
    class Parent {
      Parent(int i, String s) {
        // do stuff
    class Child extends Parent {
      Child(int i, String s) {
        super(i, s);
        // do Child stuff here
    new Parent(1, "abc");
    new Child(2, "xyz");Although that's not overriding, it sort of looks similar. Is this what you were talking about?

Maybe you are looking for