06.inheritance in SAP Classes
06.inheritance in SAP Classes
+ -
Inheritance
Inheritance is the concept of passing the behavior of one class to another class.
However they can overwrite the existing methods also add new code. Advantage of this property is
re-usability.
This means we can add additional features to an existing class without modifying it.
SUPER is the keyword used to represent the super class in oops, you can access the methods and
attributes the super class using this word super.
REDIFINATION is the keyword which is used to overwrite the parent class methods with new definition.
NOTE: In Object Oriented ABAP we have only single inheritance. There is no multiple inheritance.
Inheritance in general
Super Class Is a main class by using this we can derive a new class which will be called as Child
class.
Final Class is a class which can not be used for inheritance, it is a class property (check box under
properties of class).
If the final check box is selected, we can not use this class for inheritance, to use inheritance remove
the final check box.
Learner Questions
+ -
Go to SE24, provide name as ZCL_SAPN_SUPERCLASS, create
A pop up will open provide description, un-select final check box, save and save as local object.
Go back to methods, double click on method name and add below code.
METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.
A pop up will open provide description, click on create inheritance icon(see below image), provide
super class name.
Save, save as a local object, now you can see the inherited method, using inheritance we can
overwrite the method implementation to do this click on redifinition icon (see image below)
We will add additional functionality to get material description details along with material details, go
to attributes tab and add a attribute ls_makt-instance-public-makt.
Go back to methods, doublle click on method, uncomment the inherited code, pass parameters and
add additional code to get material descriptions.
METHOD GET_MATERIAL_DETAILS.
ENDMETHOD.
START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
Learner Questions