Abstract Class and Interface – both has there own usages. Lets explore when to use which in ABAP while developing an application in SAP.
Before jumping to the differences, lets check out the basics of both – Abstract Class and Interface.
What is an Abstract Class?
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.
Simple Abstract Class
* CLASS zcl_base_functions DEFINITION ABSTRACT. PUBLIC SECTION. METHODS: set_my_name ABSTRACT IMPORTING iv_text TYPE string . METHODS: write_name. PRIVATE SECTION. DATA: my_name TYPE string. ENDCLASS. "zcl_base_functions DEFINITION * CLASS zcl_base_functions IMPLEMENTATION. METHOD write_name. ENDMETHOD. "write_name ENDCLASS. "zcl_base_functions IMPLEMENTATION
What is an Interface?
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.
INTERFACE zif_order. methods: set_order_data IMPORTING iv_order_Data type string. methods: create_order. ENDINTERFACE. * class zcl_sales_order DEFINITION. PUBLIC SECTION. INTERFACEs: zif_order. ENDCLASS.
Since both abstract class and interface are different entity, they have few differences:
- Multiple Inheritance: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.
- New Functionality: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.
- Default Behavior:We can have a default behavior of a non-abstract method in abstract class. We can’t have any implementation in Interface as it only contains the empty stub.
- Visibility:All interface components are PUBLIC by default. For Abstract class, we can set the visibility of each component.
Based on the above mentioned differences, we can come to this recommendations:
- If want to have multiple inheritance, you need to use Interface. As we can have more than one interface in the class definition, we can achieve multiple inheritance.
- If wish to create multiple flavors of the object with some default operations, you should consider creating abstract class. As it provides you the flexibility to add the default operations directly to base class and all the subclass would get the new default operation.
- If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
- Interface should be used when working for a wide range of objects as interfaces only contain the stub, which enforces no default behavior. Abstract class should be used for classes which are closely related to each other.
- We should use Interfaces for small functionalities which can be clubbed together to derived the concrete business object.
- Interfaces, in contrary to abstract class, gain interface functionality no matter which hierachy tree they lie. So interface become common point to hierarchies which can’t normally seat one next to another. Futhermore if they require common implementation, a helper class can be utilized here. Thanks to Marcin.
Let me know, if I have missed to have any point here.
Check out all ABAP Objects Concepts
You would also like to explore more about other object oriented concepts in ABAP Objects:
- ABAP Objects: Overriding (Redefinition)
- ABAP Objects: Narrowing Cast
- ABAP Objects: Widening Cast
- Persistent Object Services – Basics
- Persistent Object Service – Example
- CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom?
- READ-ONLY attribute vs GETTER methods
- ABAP Objects Concepts – Friends
- Class-based Exceptions I – Basics
- Class-based Exceptions II – Design Time consideration
- Class based Exceptions III – Runtime flow
- Class based Exceptions IV – CLEANUP
- Become a JAVAPER – is overloading possible in ABAP?
- Override (Redefine) Static Method?
- Abstract Class vs Interface – Which to use when?
- When to use Local Class and when not to!
- Become a JAVAPER – “overriding” an attribute
- ABAP Objects – Achieve Multiple Inheritance using Interfaces
- ABAP Protected Section – Is it really Protected?
- ABAP Static vs Instance method – Which to use when?
- ABAP Objects Events – Raising and Handling
- ABAP Objects Events – SENDER, know who raised the event
- ABAP Abstract methods or Normal methods?
- ABAP 740 – Is CONSTANT not a Static Attribute anymore?