Program

REPORT Z_INHERITANCE_EX.      *name of the project

CLASS Parent_class DEFINITION. *Class name Parent_class
PUBLIC SECTION. *Access control is public
METHODS: display REDEFINITION.
DATA: data TYPE string. *Data attribute of string type
ENDCLASS.

*implementation of display method below
CLASS Parent_class IMPLEMENTATION.
METHOD display.
WRITE :/ ’parent_data=’, data.
ENDMETHOD.
ENDCLASS.

* child_class inherited from parent_class
CLASS Child_class DEFINITION INHERITING FROM Parent_class.
* starting of the public section of the class
PUBLIC SECTION.
METHOD :display REDEFINITION.
* declared subclass of string type
DATA: subclass_data TYPE string.
ENDCLASS.

*implemantion of child class
CLASS Child_class IMPLEMENTATION
METHOD display.
WRITE:/ ’Child_data=’, subclass_data. “to display message in “subclass_data”
ENDMETHOD.
ENDCLASS.

* DEFINING OBJECT REFERENCES.

DATA Parent_instance TYPE REF TO Parent_class.
DATA Child_instance TYPE REF TO Child_class.

*instance for parent
CREATE OBJECT Parent_instance.
*instance for child
CREATE OBJECT Child_instance.

*attributes setup of base and derived class
Parent_instance->data=’This is Parent_data’.
Child_instance->data=’This is Child_data’.
Child_instance->data =’Subclass_data=’This is specific_child data’.

*displaying data
CALL METHOD Parent_instance->display.
CALL METHOD Child_instance->display.




Output:

Parent_data=This is Parent_data
Child_data=This is specific_child data

SAP ABAP | Understanding Inheritance

As we know there are four pillars of object-oriented programming language. In the same way, SAP ABAP (Advanced Business Application Programming) also has four pillars: Encapsulation, Inheritance, Abstraction, and Polymorphism. In this article, we are going to learn about Inheritance in ABAP, child class, Parent class, and access control. We will also learn about the implementation of inheritance with the help of examples and redefining the methods in subclasses.

Inheritance in SAP ABAP

Similar Reads

Inheritance in SAP ABAP:

It is an important pillar of OOPS in SAP ABAP which deals with inheriting the property from parents to children. In this, the child class inherits the property of the parent class. The parent class is also known as the Base class or Super class and the child class is known as Derived class or Sub class. The data and methods of the base class are inherited by the derived class. Overwriting of the methods and adding new methods can also be performed by the child class....

Syntax of Inheritance in SAP ABAP:

CLASS DEFINITION INHERITING FROM ...

Program

REPORT Z_INHERITANCE_EX. *name of the projectCLASS Parent_class DEFINITION. *Class name Parent_class PUBLIC SECTION. *Access control is public METHODS: display REDEFINITION. DATA: data TYPE string. *Data attribute of string typeENDCLASS.*implementation of display method belowCLASS Parent_class IMPLEMENTATION. METHOD display. WRITE :/ ’parent_data=’, data. ENDMETHOD.ENDCLASS. * child_class inherited from parent_classCLASS Child_class DEFINITION INHERITING FROM Parent_class. * starting of the public section of the class PUBLIC SECTION. METHOD :display REDEFINITION. * declared subclass of string type DATA: subclass_data TYPE string. ENDCLASS.*implemantion of child classCLASS Child_class IMPLEMENTATION METHOD display. WRITE:/ ’Child_data=’, subclass_data. “to display message in “subclass_data” ENDMETHOD.ENDCLASS.* DEFINING OBJECT REFERENCES.DATA Parent_instance TYPE REF TO Parent_class.DATA Child_instance TYPE REF TO Child_class. *instance for parentCREATE OBJECT Parent_instance. *instance for childCREATE OBJECT Child_instance. *attributes setup of base and derived classParent_instance->data=’This is Parent_data’.Child_instance->data=’This is Child_data’.Child_instance->data =’Subclass_data=’This is specific_child data’.*displaying data CALL METHOD Parent_instance->display. CALL METHOD Child_instance->display....

Rules for Redefining method in Sub Class in SAP ABAP:

While redefining the method in child class then name,input parameter, return type should be suitable with the method used in parent class. “REDEFINITION” keyword should be used while redefining the child class. Need not to write the interface again for the child while redefining. If the access control of method in parent class is public then redefinition of child class method should be either public or protected....

Access control in inheritance in SAP ABAP :-

When a sub-class is derived from any super-class in SAP ABAP then inheritance can be done by different accessible control. There are three accessible control in inheritance...

Conclusion:

Inheritance is one of the important OOPS concepts that is used to inherit the property from parent class to child class. Redefinition of the child can also be done by following some rules which are mentioned above in the article. We have three access controls (Public, Private, and Protected) which play a vital role in inheritance and also determine the accessibility of the class members....