SAP ABAP | Understanding Inheritance
Last Updated :
28 Nov, 2023
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
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.
- Child class: - The class that inherits the property of another class is known as the child/driver/subclass.
- 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:
- 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.
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.
- Rule 1:- We have used the same method name (display) and return type (string) in the child class and parent class.
- Rule 2:- "REDEFINITION" keyword is used to redefine the method in child class.
- 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
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.
|
Public
| Protected
| Private
|
Yes
| Yes
| Yes
|
Yes
| Yes
| No
|
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.
Similar Reads
SAP Advanced Business Application Programming (ABAP)
SAP ABAP stands for Advanced Business Application Programming. 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 i
6 min read
What is SAP ABAP: A Brief Overview
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 bu
9 min read
SAP ABAP | Basic Syntax & Statements
The German software company SAP created the high-level programming language, ABAP (Advanced Business Application Programming) primarily, this language serves as a tool for developing applications within the SAP R/3 system. Designed with simplicity and ease of learning in mind, ABAP syntax allows eff
9 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. Sy
7 min read
SAP ABAP Keywords
The core of many global enterprises, SAP ABAP stands for Advanced Business Application Programming. It is the heart and soul of SAP systems. It gives users the ability to expand and modify SAP apps to satisfy particular business needs. The fundamental building blocks of SAP ABAP are its keywords, wh
5 min read
SAP ABAP | Constants & Literals Explained
In the world of SAP ABAP (Advanced Business Application Programming), the use of Constants and Literals is necessary for the effective handling of data. Literals are used to denote specific data types such as numbers, characters, strings, and boolean values. What are Literals?Literals in programming
7 min read
SAP ABAP | Data Types
Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of t
6 min read
Relational operators in SAP ABAP
Relational operators in SAP ABAP are symbols or combinations of symbols that compare values and return a Boolean result (either true or false). These operators allow developers to establish relationships between variables, constants, or expressions, facilitating decision-making processes in the prog
6 min read
Operators in SAP ABAP
High-level programming languages like SAP ABAP (Advanced Business Application Programming) are used to create apps in the SAP environment. Operators are essential for the execution of many different operations in SAP ABAP, ranging from straightforward arithmetic computations to complex logical analy
7 min read