Open In App

SAP ABAP | Understanding Inheritance

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

sap-banner

Inheritance in SAP ABAP

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.

Real-life example: – Let’s consider a father and a child. Father’s name is Shekhar and son’s name is Kanu. Shekhar has brown skin, black hair, and grey eyes. Kanu also has grey eyes. So here, Kanu inherited the property of grey eyes from his father. Kanu acts as a derived class and Shekhar acts as a base class.

  1. Child class: – The class that inherits the property of another class is known as the child/driver/subclass.
  2. Parent class: – The class whose property is being inherited by another class is known as the Parent class.

Syntax of Inheritance in SAP ABAP:

CLASS <sub-class> DEFINITION INHERITING FROM <super-class>

  • <sub-class> : Specifies the subclass or derived class.
  • <super-class> : Specifies the super class or parent class or base class.

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

Rules for Redefining method in Sub Class in SAP ABAP:

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

Redefining Methods in Subclasses:

     CLASS Parent_class DEFINITION.
PUBLIC SECTION.
METHODS: display.
DATA: parent_data TYPE string.
ENDCLASS.

CLASS Child_class DEFINITION INHERITING FROM Parent_class.
PUBLIC SECTION.
METHOD:display REDEFINITION.
DATA: child_data TYPE string.
ENDCLASS.

Lets see how the rules of redefinition have been used in the above code.

  1. Rule 1:- We have used the same method name (display) and return type (string) in the child class and parent class.
  2. Rule 2:- “REDEFINITION” keyword is used to redefine the method in child class.
  3. Rule 3:- Access control of parent class and child class is also same as shown in the code i.e. “PUBLIC”.

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

  • Public
  • Private
  • Protected

The type of accessible control needs to define while inheriting the property of parent class depends on the need or demand of the programmer. “Public” access control is used widely while the uses of “Protected” and “Private” access control is very less. The table drawn below shows the accessible of class members with different accessible control.

ACCESS

Access control key

Public

Protected

Private

Same Class

Yes

Yes

Yes

Derived Class

Yes

Yes

No

Outside Class

Yes

No

No

  • Public:- The member which is declared public can be accessed from inside and outside the class.
  • Protected:- The member which is defined as protected can be accessed within the class and it’s sub-class only.
  • Private:- The member which is defined as private can be accessed with the class only.

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads