- SAP ABAP - Web Dynpro
- SAP ABAP - Business Add-Ins
- SAP ABAP - User Exits
- SAP ABAP - Customer Exits
- SAP ABAP - SAPscripts
- SAP ABAP - Smart Forms
- SAP ABAP - Dialog Programming
- SAP ABAP - Report Programming
- SAP ABAP - Object Events
- SAP ABAP - Interfaces
- SAP ABAP - Encapsulation
- SAP ABAP - Polymorphism
- SAP ABAP - Inheritance
- SAP ABAP - Classes
- SAP ABAP - Objects
- SAP ABAP - Object Orientation
- SAP ABAP - Deleting Internal Tables
- SAP ABAP - Reading Internal Tables
- SAP ABAP - Copying Internal Tables
- ABAP - Populating Internal Tables
- SAP ABAP - Creating Internal Tables
- SAP ABAP - Internal Tables
- SAP ABAP - Native SQL Overview
- SAP ABAP - Open SQL Overview
- SAP ABAP - Include Programs
- SAP ABAP - Function Modules
- SAP ABAP - Macros
- SAP ABAP - Subroutines
- SAP ABAP - Modularization
- SAP ABAP - Lock Objects
- SAP ABAP - Search Help
- SAP ABAP - Views
- SAP ABAP - Structures
- SAP ABAP - Tables
- SAP ABAP - Data Elements
- SAP ABAP - Domains
- SAP ABAP - Dictionary
- SAP ABAP - Exception Handling
- SAP ABAP - Formatting Data
- SAP ABAP - Date & Time
- SAP ABAP - Strings
- SAP ABAP - Decisions
- SAP ABAP - Loop Control
- SAP ABAP - Operators
- SAP ABAP - Constants & Literals
- SAP ABAP - Variables
- SAP ABAP - Data Types
- SAP ABAP - Basic Syntax
- SAP ABAP - Screen Navigation
- SAP ABAP - Environment
- SAP ABAP - Overview
- SAP ABAP - Home
SAP ABAP Useful Resources
- SAP ABAP - Discussion
- SAP ABAP - Useful Resources
- SAP ABAP - Quick Guide
- SAP ABAP - Questions Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SAP ABAP - Inheritance
One of the most important concepts in object oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an apppcation. This also provides an opportunity to reuse the code functionapty and fast implementation time.
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.
An object of one class can acquire the properties of another class.
Derived class inherits the data and methods of a super class. However, they can overwrite methods and also add new methods.
The main advantage of inheritance is reusabipty.
The inheritance relationship is specified using the ‘INHERITING FROM’ addition to the class definition statement.
Following is the syntax −
CLASS <subclass> DEFINITION INHERITING FROM <superclass>.
Example
Report ZINHERITAN_1. CLASS Parent Definition. PUBLIC Section. Data: w_pubpc(25) Value This is pubpc data . Methods: ParentM. ENDCLASS. CLASS Child Definition Inheriting From Parent. PUBLIC Section. Methods: ChildM. ENDCLASS. CLASS Parent Implementation. Method ParentM. Write /: w_pubpc. EndMethod. ENDCLASS. CLASS Child Implementation. Method ChildM. Skip. Write /: Method in child class , w_pubpc. 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.
The above code produces the following output −
This is pubpc data Method in child class This is pubpc data
Access Control and Inheritance
A derived class can access all the non-private members of its base class. Thus super class members that should not be accessible to the member functions of sub classes should be declared private in the super class. We can summarize the different access types according to who can access them in the following way −
Access | Pubpc | Protected | Private |
---|---|---|---|
Same calss | Yes | Yes | Yes |
Derived class | Yes | Yes | No |
Outside class | Yes | No | No |
When deriving a class from a super class, it can be inherited through pubpc, 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 pubpc inheritance is commonly used. The following rules are appped while using different types of inheritance.
Pubpc Inheritance − When deriving a class from a pubpc super class, pubpc members of the super class become pubpc 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 pubpc and protected members of the super class.
Protected Inheritance − When deriving from a protected super class, pubpc and protected members of the super class become protected members of the sub class.
Private Inheritance − When deriving from a private super class, pubpc and protected members of the super class become private members of the sub class.
Redefining Methods in Sub Class
The methods of the super class can be re-implemented in the sub class. Few rules of redefining methods −
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.
The pseudo reference super can only be used in redefined methods.
Example
Report Zinheri_Redefine. CLASS super_class Definition. Pubpc Section. Methods: Addition1 importing g_a TYPE I g_b TYPE I exporting g_c TYPE I. ENDCLASS. CLASS super_class Implementation. Method Addition1. g_c = g_a + g_b. EndMethod. ENDCLASS. CLASS sub_class Definition Inheriting From super_class. Pubpc Section. METHODS: Addition1 Redefinition. ENDCLASS. CLASS sub_class Implementation. Method Addition1. g_c = g_a + g_b + 10. EndMethod. 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 −
![Re-implemented os Sub Class](/sap_abap/images/reimplemented_sub_class.jpg)
Redefinition Demo 29Advertisements