SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented Programming in ABAP - Fundamentals of inheritance

Object Oriented Programming in ABAP - Fundamentals of inheritance

The concept of inheritance is used to incorporate the properties of an existing class into a new class .The beauty of this feature is that the methods and the attribute of the existing
class need not to be coded into the new class, as well as new features can be added into the the new class.
In OOP inheritance can be multiple as well as multi-level, but in ABAP only multi-level inheritance is possible. In Multiple Inheritance a new class can inherit properties of several class.

Where as multi-level inheritance is an inheritance of hierarchal fashion. In OOP terminology the parent class is called the super class and the derived class is called the subclass.

 

Fundamentals of inheritance

 

If you do not add any new declarations to the subclass, it contains the same components as the superclass. However, only the public and protected components of the superclass are
visible in the subclass. Although the private components of the superclass exist in the subclass, they are not visible.

 

private Section is not visible in subclass

 

You can declare private components in a subclass that have the same names as private components of the superclass. Each class works with its own private components.

Methods that a subclass inherits from a superclass use the private attributes of the superclass, and not any private components of the subclass with the same names.
If the superclass does not have a private visibility section, the subclass is an exact replica of the superclass. However, you can add new components to the subclass. This allows you to turn
the subclass into a specialized version of the superclass.

Member access and inheritance

As we know that in inheritance the subclass inherits the property of the super class, now the question comes can the subclass inherit all the property of the super class ?

The answer is NO, only the public and the protected members are inheritable by the sub class.

Now what happens if any component of the superclass has the same name that of the sub class ?

This is only possible in case of private components, the private components of a super class or subclass does not interfere or interact they are sole property of the class in which they are declared .
Where as public or protected components must be unique i.e they must not have same name .

Another question might come up, suppose a super class method is called by the subclass, and suppose it takes some input parameters that are private, and the input parameters are explicitly defined both in sub class as well as in super class, then during the function call which components does it uses ,is it of super class or that of subclass ?
Well it uses the components of the super class.

 

Method Redefinition

The method that has already been defined in the superclass can be redefined within the subclass .When a super class method is redefined within the sub–class then the method name export import parameters remain the same but the method implementation changes. And whenever the redefined method is called from any instance of the sub class then the new implementation comes into picture. The earlier implementation that already existed in the super class is shadowed by the new implementation.

 

Method Redefinition

 

Accessibility of components by use of super


From the above diagram it can be seen that if a method is redefined then, the uniqueness of the method is implicitly defined by the system, this prevents the confusion during the
method call. When a redefined method is called by the super class then the method implementation in the super class is called and when the redefined method is called from the
sub class then the newly defined version is called.

Concept of super and final

Super
As we have seen in method redefinition how the system manages the uniqueness of the method name, it has been also shown which method definition is called when the method is
called from subclass or superclass. But what if we want to call the method implementation of the superclass from the subclass. We can do this by using the ABAP key word super, super is a reference to the super class and with this reference we can call the method implementation in the super class.

Final
We have seen that any class can be inherited by any other class ,but suppose we want to prevent a class from being inherited then what do we do??
We can do this by adding the key word final with class. Any class that is declared bas final cannot be inherited, or in other words final puts an end to the inheritance tree.
Similarly if we want to put a stop to any particular method, from being inherited we can do it by declaring as final. A point to note a final method cannot be redefined in the subclass.

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in