Super

From Abap2Java

Jump to: navigation, search

ABAP

The keyword super allows to access constructors and methods of super classes. The super class is the class, from which a class inherits (with the keyword INHERITING FROM).

CLASS A DEFINITION.
  PROTECTED SECTION:
    DATA a TYPE I.
    DATA b TYPE I.
  PUBLIC SECTION:
    METHODS do_this.
ENDCLASS. 

CLASS B DEFINITION INHERITING FROM A.
  PROTECTED SECTION:
    "DATA a TYPE I. "Not possible to hide field 'a' from super class
  PUBLIC SECTION:
    METHODS CONSTRUCTOR.
    METHODS do_this REDEFINITION.
ENDCLASS.

CLASS B IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    "if the super constructor is called, it must be the first statement in the constructor
    super->constructor( ).
    me->a = 42.
  ENDMETHOD.
  METHOD do_this.
    "access the overwritten method
    super->do_this( ).
    
    "super->a = 12. "not possible, use me->a.
    "super->b = 2.  "not possible, use me->b. 
  ENDMETHOD.
ENDCLASS.

Java

The keyword super allows to access constructors, methods and fields of super classes. The super class is the class, from which a class inherits (with the keyword extends).

class A {
   protected int a;
   protected int b;   
   public void doThis() { ... } 
}

class B extends A {
  
   // hiding the field 'a' from super class
   protected int a;

   //constructor:
   B() {
      // if the super constructor is called, 
      // it must be the first statement in the constructor
      super();
      this.a = 42;
   }

   // Overwriting the method doThis
   public void doThis() {
       
      // access to the hidden field a in super class
      super.a = 1;
      
      // access the overwritten method
      super.doThis();

      // as the variable b is not hidden by class B,
      // these expression are equivalent.
      super.b = 2;
      this.b = 2;
   } 
}
Personal tools