Class in ABAP
All of you already know what a class is and how it is created in ABAP. You can refer to the previous post to know basic terminologies.
Here I will talk about some of the basics associated with these classes
Abstract Class vs Interfaces
Abstract Class is a special kind of class which can’t be instantiated. We can only instantiate the subclasses of the Abstract class if they are not abstract. Abstract class should at least contain one abstract method. Abstract methods are methods without any implementation – only a declaration. We can certainly define the variables referencing to Abstract class and instantiate with specific subclass at runtime
An interface is not a class. It is an entity which can’t have implementation. An interface can only contain empty method declaration and components. Interface components are always public. We can later change the visibility of the components within the implementing class using the ALIASES.
Differences
- We can achieve multiple inheritance using Interfaces. Since ABAP doesn’t support more than one Super class, we can have only one abstract class as Super class.
- If we add a new method in the Interface, all the implementing classes have to implement this method. If we don’t implement the method, it would result into Run-time error.
- For Abstract class, if we add a non-abstract method, its not required to redefine that in each and every inherited class.
- We can have a default behavior of a non-abstract method in abstract class.
- All interface components are PUBLIC by default. For Abstract class, we can set the visibility of each component.
General Info
- Local classes are defined within an ABAP program. Local classes and interfaces can only be used in the program in which they are defined. When you use a class in an ABAP program, the system first searches for a local class with the specified name. If it does not find one, it then looks for a global class.
- Local Class can also be defined in the Global Class. We can define the local class in the Local Types section of the Class Editor. This can be implemented in the Implementation section of the class editor.
- When we want to enhance the class methods system would create Local Class to achieve this functionality.
- Local classes should be used in the same program and not accessible to other programs.
Proxy
- Proxy object only instantiate the object when it is required. If the object is not needed, it would not be created. Once the object is created, all the future operations would be carried over on the “Real” object instead of on the proxy object. Both – proxy and real – objects would implement the same interface. Thus both object can perform same operations seamlessly. Also the proxy object can pass all the signature to the real object.
Multiple Inheritance
Feature in which a class can inherit components – methods, attributes, events – from more than one Superclass. In other words, you can define a class which has multiple “parent” class. ABAP objects doesn’t support multiple inheritance using more than one class.
Since we can include more than one interface while declaring a class using keyword INTERFACES, you can achieve the multiple inheritance in ABAP.