Concept of super and final in ABAP Objects

Go down

Concept of super and final in ABAP Objects

Post  Uma_ABAP on Sun Apr 01, 2012 1:10 pm


Super As we have seen in method redefinition how the system manages the uniqueness of the method name ,it has been also shown which method definition is called when the method is called from subclass or superclass. But what if we want to call the method implementation of the superclass from the subclass.

We can do this by using the ABAP key word super, super is a reference to the super class and with this reference we can call the method implementation in the super class.

Code:

CLASS father DEFINITION.
PUBLIC SECTION.
METHODS: my_method.
ENDCLASS.

CLASS father IMPLEMENTATION.
METHOD my_method.
Write:/ ‘ this is father method’.
ENDMETHOD.
ENDCLASS.

CLASS son DEFINITION INHERITING FROM father.
PUBLIC SECTION.
METHODS:my_method REDEFINITION,
Call_my_method.
ENDCLASS.

CLASS son IMPLEMENTATION.
METHOD my_method.
Write:/ ‘father method redefined in son’.
ENDMETHOD.

METHOD call_my_method.
Call super->my_method. “ the method definition of father will be called
ENDMETHOD.
ENDCLASS.

Final
We have seen that any class can be inherited by any other class ,but suppose we want to prevent a class from being inherited then what do we do??

We can do this by adding the key word final with class. Any class that is declared bas final cannot be inheritated ,or in other words final puts an end to the inheritance tree.

Similarly if we want to put a stop to any particular method ,from being inherited we can do it by declaring as final. A point to note a final method cannot be redefined in the subclass.

Code:

CLASS test DEFINITION FINAL. “ this class cannot be inherited
ENDCLASS.
CLASS test IMPLEMENTATION.
ENDCLASS.


Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum