Difference between Abstract and Interface in OOPS ABAP

Before going through the differences between Abstract and Interface in Object Oriented ABAP please check below links to get understanding of Abstract and Interface concepts.




Difference between Abstract and Interface in OOPS ABAP

Simple Abstract class Example:

CLASS zcl_sample_code DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS: set_name ABSTRACT.
    METHODS: write_name.
  PRIVATE SECTION.
    DATA: my_name TYPE string.
ENDCLASS.                    "zcl_sample_code DEFINITION

CLASS zcl_sample_code IMPLEMENTATION.
  METHOD write_name.
  ENDMETHOD.              "write_name
ENDCLASS.                    "zcl_sample_code IMPLEMENTATION


Interface Example

INTERFACE zif_sales_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 between Abstract and Interface

Abstract Interface
It is a class which can't be instantiated. Can be instantiated only from subclass if subclass is not abstract It is an entity which contains only declarations of methods and attributes
Multiple Inheritance is not supported. As OO ABAP supports only one super class we can have only one abstract class as super class. Multiple Interfaces can be used in class definition and hence Multiple Inheritance is supported
It is not mandatory to redefine the non abstract method added in abstract class in all subclasses that inherits the abstract super class Any new method included in interface must be implemented in all the classes which uses the interface otherwise runtime error will occur
Since abstract class can contain abstract and non abstract methods, we can include default logic in non abstract method of abstract super class. All subclasses will inherit this logic. Since interface contains only empty method declarations we can't include a default logic in Interface
We can set the visibility of each component in Abstract class All interface components are public by default

Post a Comment

Previous Post Next Post