SAP ABAP Inheritance - SAP ABAP

What is Sap abap inheritance? What are its objectives?

In SAP ABAP inheritance is considered as one of the main concepts in object oriented programming. Inheritance describes the relationship between two classes. i.e class within the class. It helps to create and maintain an application easily. This also gives an opportunity to reuse the code functionality and implementation method.

In ABAP inheritance is involved with two classes, i.e parent class and sub class. So when the user creates a class, the subclass inherits the data from the parent class. This prevents the programmer to include the data details again and again. Instead of doing that, the data can be directly taken from the existing 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 reusability.

The inheritance relationship is specified using the ‘INHERITING FROM’ addition to the class definition statement.

Following is the syntax −

Example

The above code displays the following output −

Access Control and Inheritance

A super class or parent class has the capacity to access all the non-private members of its sub class. Due to the encapsulation of restricted data, the private members of the super class cannot be accessible to the member functions of sub classes. You can see below in the tabular form of different data types which is used by the users are-

Access Public Protected Private
Same calss Yes Yes Yes
Derived class Yes Yes No
Outside class Yes No No

The derived super class inherits its members through public, protected or private inheritance. The type of inheritance is determined by the access specifier as explained above. Rarely, the user uses the protected or private inheritance, but the most widely used is public inheritance. The following rules are applied while using different types of inheritance.

  • Public Inheritance – when the super class is derived from the public inheritance, public members of the super class is inherited from the public members of the sub class and protected members of the super class is inherited from protected members of the sub class. Super class's never accesses private members directly from a sub class, but can be accessed through calls to the public and protected members of the super class.
  • Protected Inheritance − When deriving from a protected super class, public and protected members of the super class inherits protected members of the sub class.
  • Private Inheritance − When deriving from a private super class, public and protected members of the super class inherits 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 should be in the same section as that of original method.
  • If you redefine a method, you need not enter the details of the interface again in the subclass, but you should mention only the name of the method.
  • Within the redefined method, with the super reference you can access components of the direct super class.
  • The pseudo reference super can only be used in redefined methods.

Example

After executing F8, if we enter the values 9 and 10, the above code produces the following output −

reimplemented_sub_class

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAP ABAP Topics