Abstract Class vs Interface – Which to use when?

By | January 4, 2012 | ABAP Objects, OO Concepts | 173,461 | 12

Abstract Class and Interface – both has there own usages. Lets explore when to use which in ABAP while developing an application in SAP.

Basics

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.

Simple Interface

 
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.  
 

Differences

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.

Recommendations

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:

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

12 Comments

  • Kesav

    Thanks for the posts … Real time explanations like this are easy to understand. As Multiple inheritance implies inheriting the behaviors from more than one super class , I still do not get the clear idea how multiple interface in a definition would provide Multiple inheritance.
    Please clear it.

  • marcin

    Hi Kesav,

    By “multiple inheritance” it is meant that each class can implement several not related interfaces. In ABAP (and in Java too) it is not allowed for a class to have several parent classes. Only one parent in hierarchy tree is allowed.

    Alternative to this is “multiple inheritance” by implementing multiple interfaces. Such class gains functionality of all the interfaces it implements (so as if it has multiple parents).

    @Naimesh
    One addition to your conclusion. 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.

    Regards
    Marcin

  • Naimesh Patel

    Hello Keshav,

    Echoing what Marcin has said – When we create inheritance from a Super Class, we can have only one Super Class. But We can have more than one interfaces in a class declaration.

    At design time client doesn’t need to know who actually implements the interface. It can call all the methods of the interface without a doubt. At runtime, a specific class would be instantiated which has implemented the interface.

    I’ll try to write a post on that, soon…

    Regard,s
    Naimesh Patel

  • Kesav

    Thanks Marcin & Naimesh,

    Thanks for making it clear. I was actually confused with the term inheritance ( Where a super class comes into picture ). Here its meant that multiple interfaces( considered as parent to the implementing class ) can be implemented in a same class.

    BR
    Kesav

  • Kesav

    Marcin & Namiesh,

    First of all thanks for clearing my doubts. I was actually confused with the term Inheritance( Where a super class comes into picture ).

  • […] an interface or an Abstract class based on the requirement of the […]

  • fewtron

    seriously look like you have quality of teaching also,i got very useful explanation on your topics,but one thing you did not tell the scope of both (abstraction and interface). Means give example of SD or MM or any of your favourite module and make things more clearer and practical for the people newbie like me …

  • Siddharth

    Hi Namiesh,

    I feel through the concept of friends we can achieve the multiple inheritance. If there are three classes A, B, C. All of these classes have m1() method whatever its scope has. Now each class define Class D as there friend that means now class D has availability of method m1() from all these class. I do not know how will it differentiate in run time for which class(A,B,C) method’s m1() will called.

    If class D also inheriting class E which have method m1() then how will we call m1() method of E. Please clarify because i feel with that way i can achieve multiple inheritance.

    Friends is new concept in OO ABAP which breach the law of private access modifier.

    Thanks alot for such a great blog. First time i am visiting but now i am reading each article because these are real time.

  • What is instance and instantiate means respectively?

  • sumanth

    thanks Naimesh for this blog.

    i have checked this specific question with colleagues and no one gave me satisfactory answer. This clears my mind almost all the points and next usage should be easy for me.

  • Girish

    Hi Naimesh,
    It’s Really Helpful for me..
    Could u please give some real time Example on abstract and interface Concepts.

  • Hi,

    i find your work very useful, you deliver us a very high quality content.

    +1 idea to extend your list:

    If you use an interface, you need to call the method as interface~method or by using an alias. If you use an abstract superclass, then you can use the method name. This can be an advantage.
    Otherwise, we have in ABAP no possibility to use methods with the same name and different signature, therefore using an interface can be an advantage. Example: class method: save , interface method: save can be different methods with different signatures.

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.