Inheritance defines the relationship between classes, in which a class (subclass) uses the structure and behavior that has already been defined in one or more other classes (superclasses). So simply this means "Inheritance is about reuse!".
Allow me to use a concrete example to explain inheritance: Collection.
A collection is any number of objects (more precisely object references). However, there could be many types of collection. Therefore, I will implement each type of collection as a class. In principle this approach is correct. However, you will soon establish that all collections have several components in common like:
1. Each class requires a method in order to add objects to a collection.
2. Each class requires a method in order to delete objects from a collection.
3. Each class has a method which identifies the number of object references in the collection and so on.
Inheritance is the solution to this situation. You implement all of the similarities in the class which is Superclass. You then implement the individual types of collection in their own classes which are Subclassesof the Superclass. As a subclass, these classes inherit all of the components of the Superclass. Attributes, methods and events are inherited. In addition, you can implement additional attributes, methods and events in the subclass.
POLYMORPHISM
Polymorphism occurs, where classes implement the same functionality with different methods (one functionality, several methods but the same name). This can occur via an inheritance relationship, in that the methods belonging to the superclass are redefined in the subclasses and implemented differently. ABAP Objects requires the method names to be the same and the signature to be the same (signature = method interface).
Polymorphism can be achieved in 2 ways:
(1) Two independent classes implement methods with the same names and the same signature with the intention, that the methods should be called dynamically from a third location.
(2) A superclass implements a method, and in a subclass you want to re-implement the same method, as the superclass implementation is not suitable for the subclass.
The first scenario will not occur very often in ABAP Objects, as the interface concept was created precisely for such cases.
The first scenario will not occur very often in ABAP Objects, as the interface concept was created precisely for such cases.
Code listing for: Z_008_INHERIT_POLY
Description: EXAMPLE OF INHERITANCE & POLYMORPISM
Description: EXAMPLE OF INHERITANCE & POLYMORPISM
REPORT Z_008_INHERIT_POLY.
TYPES: BEGIN OF TY_NAME,
NAME(50) TYPE C,
END OF TY_NAME.
TYPES: TY_NAMES TYPE TY_NAME OCCURS 0.
*----------------------------------------------------------------
* CLASS CL_COLLECTION DEFINITION
*----------------------------------------------------------------
CLASS CL_COLLECTION DEFINITION.
PUBLIC SECTION.
METHODS: ADD IMPORTING IM_NAME TYPE TY_NAME,
DISPLAY.
PROTECTED SECTION.
DATA IT_NAMES TYPE TY_NAMES.
ENDCLASS. "CL_COLLECTION DEFINITION
*----------------------------------------------------------------
* CLASS CL_COLLECTION IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_COLLECTION IMPLEMENTATION.
METHOD ADD.
APPEND IM_NAME TO IT_NAMES.
ENDMETHOD. "ADD
METHOD DISPLAY.
DATA: CNT TYPE I.
DESCRIBE TABLE IT_NAMES LINES CNT.
IF CNT > 0.
DATA WA_NAME LIKE LINE OF IT_NAMES.
ULINE.
WRITE 10 'DISPLAYING DATA'.
ULINE.
WRITE 10 'NAMES'.
WRITE 10 'NAMES'.
ULINE.
LOOP AT IT_NAMES INTO WA_NAME.
WRITE /10 WA_NAME-NAME.
ENDLOOP.
ENDIF.
ENDMETHOD. "DISPLAY
ENDCLASS. "CL_COLLECTION IMPLEMENTATION
*----------------------------------------------------------------
* CLASS CL_NAME DEFINITION
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION INHERITING FROM CL_COLLECTION.
PUBLIC SECTION.
METHODS: ADD REDEFINITION. " THIS IS CALLED POLYMORPISM.
ENDCLASS. "CL_NAME DEFINITION
*----------------------------------------------------------------
* CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
METHOD ADD.
" NO DOUBLE ENTRIES ARE ALLOWED.
READ TABLE IT_NAMES WITH KEY NAME = IM_NAME-NAME
TRANSPORTING NO FIELDS.
IF SY-SUBRC <> 0.
CALL METHOD SUPER->ADD
EXPORTING
IM_NAME = IM_NAME.
ENDIF.
ENDMETHOD. "ADD
ENDCLASS. "CL_NAME IMPLEMENTATION
DATA: INSTANCE TYPE REF TO CL_NAME,
ST_NAME TYPE TY_NAME.
START-OF-SELECTION.
CREATE OBJECT INSTANCE.
ST_NAME-NAME = 'VINO'.
CALL METHOD INSTANCE->ADD
EXPORTING
IM_NAME = ST_NAME.
WRITE / 'ADDED VINO FOR THE 1ST TIME.'.
ST_NAME-NAME = 'BALAJI'.
CALL METHOD INSTANCE->ADD
EXPORTING
IM_NAME = ST_NAME.
WRITE / 'ADDED BALAJI FOR THE 1ST TIME.'.
ST_NAME-NAME = 'HARI'.
CALL METHOD INSTANCE->ADD
EXPORTING
IM_NAME = ST_NAME.
WRITE / 'ADDED HARI FOR THE 1ST TIME.'.
ST_NAME-NAME = 'VINO'.
CALL METHOD INSTANCE->ADD
EXPORTING
IM_NAME = ST_NAME.
WRITE / 'ADDED VINO FOR THE 2ND TIME.'.
ST_NAME-NAME = 'BALAJI'.
CALL METHOD INSTANCE->ADD
EXPORTING
IM_NAME = ST_NAME.
WRITE / 'ADDED BALAJI FOR THE 2ND TIME.'.
ST_NAME-NAME = 'HARI'.
CALL METHOD INSTANCE->ADD
EXPORTING
IM_NAME = ST_NAME.
WRITE / 'ADDED HARI FOR THE 2ND TIME.'.
SKIP.
SKIP.
CALL METHOD INSTANCE->DISPLAY.
Program Output : 008
ADDED VINO FOR THE 1ST TIME.
ADDED BALAJI FOR THE 1ST TIME.
ADDED HARI FOR THE 1ST TIME.
ADDED VINO FOR THE 2ND TIME.
ADDED BALAJI FOR THE 2ND TIME.
ADDED HARI FOR THE 2ND TIME.
ADDED HARI FOR THE 2ND TIME.
-----------------------------------------------------------------
DISPLAYING DATA
-----------------------------------------------------------------
NAMES
-----------------------------------------------------------------
VINO
BALAJI
HARI
No comments:
Post a Comment