Inheritance is the concept of passing the behavior of a class to another class.
- You can use an existing class to derive a new class.
- Derived class inherits the data and methods of a super class.
- However they can overwrite the methods existing methods and also add new once.
- 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.
*&-----------------------------------------------------*
*& 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.
Save it, check it, activate it and execute it.
Then the output is like this.
No comments:
Post a Comment