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.



Previous Article
Next Article

Similar Reads

SAP ABAP: A complete Guide to Events in ABAP Report
SAP created the high-level programming language known as SAP ABAP (Advanced Business Application Programming) to be used for creating business applications. Reports are an essential tool for extracting and presenting data in SAP development. To manage the execution flow and improve the functionality of their systems, developers must comprehend the
5 min read
SAP S/4 HANA Vs SAP HANA | Difference between SAP S/4 HANA , SAP HANA
SAP S/4HANA is the most recent ERP suite, with advanced architecture allowing for real-time processing. SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP S/4 HANA?SAP, which stands f
3 min read
SAP HANA Vs SAP ECC | Difference between SAP HANA and SAP ECC
SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP HANA?SAP HANA stands for High-performance analytic appliance, which is faster than any other database management system that we use
2 min read
SAP MM Vs SAP SRM | Difference Between SAP MM and SAP SRM
SAP MM helps companies optimize their material and logistics management procedures, guaranteeing effective and economical handling of items. SAP SRM stands for Supplier Relationship Management and is an efficient tool that helps companies optimize procurement operations and helps to improve relationships with suppliers and other business vendors. W
2 min read
SAP EWM Vs SAP WM | Difference Between SAP EWM and SAP WM
SAP EWM is a version created by enhancing the SAP Warehouse Management (SAP WM) solution, one of the world’s most popular warehouse management applications, with new features. What is SAP EWM?The SAP EWM solution was created to organize and manage goods storage in warehouses, distribution centers, and logistical centers. Its primary goal is to impr
3 min read
SAP ABAP | Understanding Variables
What is a Variable?Variable are named data objects that refer to the memory location allocated during the program execution for processing. As the name indicates, users can use refer to the range of values that may be stored inside and the range of actions that can be carried out on the variable. Syntax:DATA &lt;f&gt; TYPE &lt;type&gt; VALUE &lt;va
8 min read
SAP ABAP | Understanding Object
SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which includes enterprise resource planning (ERP) systems and other business software solutions. C++ is used to implemen
4 min read
Diffrence Between SAP and SAP ABAP(Advanced Business Application Programming)
In this article, we are going to learn the difference between an SAP and SAP ABAP. SAP ABAP is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL). [caption width="800"] SAP Vs SAP ABAP(Advanced Business Application Programming)[/caption] What is
2 min read
Difference Between SAP ERP and SAP ABAP
In this article, we are going to learn the difference between an SAP ERP and SAP ABAP. SAP ABAP is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL). SAP Enterprise Resource Planning (ERP) systems are essential for simplifying and maximizing a
2 min read
Difference between SAP S/4 HANA , SAP HANA and SAP ECC
SAP S/4HANA is the most recent ERP suite, with advanced architecture allowing for real-time processing. SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. [caption width="800"]Difference between
3 min read