36 - SAP ABAP - Inheritance
36 - SAP ABAP - Inheritance
When creating a class, instead of writing completely new data members and methods,
the programmer can designate that the new class should inherit the members of an
existing class. This existing class is called the base class or super class, and the new
class is referred to as the derived class or sub class.
The inheritance relationship is specified using the ‘INHERITING FROM’ addition to the
class definition statement.
Example
Report ZINHERITAN_1.
CLASS Parent Definition.
PUBLIC Section.
Data: w_public(25) Value 'This is public data'.
Methods: ParentM.
ENDCLASS.
https://www.tutorialspoint.com/sap_abap/sap_abap_inheritance.htm 1/4
Page 2 of 4
EndMethod. ENDCLASS.
Start-of-selection.
Data: Parent Type Ref To Parent,
Child Type Ref To Child.
Create Object: Parent, Child.
Call Method: Parent→ParentM,
child→ChildM.
When deriving a class from a super class, it can be inherited through public, protected or
private inheritance. The type of inheritance is specified by the access specifier as
explained above. We hardly use protected or private inheritance, but public inheritance is
commonly used. The following rules are applied while using different types of
inheritance.
https://www.tutorialspoint.com/sap_abap/sap_abap_inheritance.htm 2/4
Page 3 of 4
Public Inheritance − When deriving a class from a public super class, public
members of the super class become public members of the sub class and
protected members of the super class become protected members of the sub
class. Super class's private members are never accessible directly from a sub
class, but can be accessed through calls to the public and protected members of
the super class.
Protected Inheritance − When deriving from a protected super class, public
and protected members of the super class become protected members of the sub
class.
Private Inheritance − When deriving from a private super class, public and
protected members of the super class become private members of the sub class.
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
The redefinition statement for the inherited method must be in the same section
as the definition of the original method.
If you redefine a method, you do not need to enter its interface again in the
subclass, but only the name of the method.
Within the redefined method, you can access components of the direct super
class using the super reference.
Example
Report Zinheri_Redefine.
CLASS super_class Definition.
Public Section.
Methods: Addition1 importing g_a TYPE I
g_b TYPE I
exporting g_c TYPE I.
ENDCLASS.
https://www.tutorialspoint.com/sap_abap/sap_abap_inheritance.htm 3/4
Page 4 of 4
ENDCLASS.
Start-Of-Selection.
Parameters: P_a Type I, P_b TYPE I.
Data: H_Addition1 TYPE I.
Data: H_Sub TYPE I.
Data: Ref1 TYPE Ref TO sub_class.
Create Object Ref1.
Call Method Ref1→Addition1 exporting g_a = P_a
g_b = P_b
Importing g_c = H_Addition1.
Write:/ H_Addition1.
After executing F8, if we enter the values 9 and 10, the above code produces the
following output −
Redefinition Demo
29
https://www.tutorialspoint.com/sap_abap/sap_abap_inheritance.htm 4/4