Sunday, November 4, 2012

Abstract Class vs Interface – Which to use when?


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.

ABAP Syntax Highlighter

 
*
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.

ABAP Syntax Highlighter

 
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:
Advertisement
  • 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.
Post a Comment