Abap objects interface example

Ever wondered how to generate the ALV Grid on the list generated by WRITE statement. Lets see today .

Why NOT to have a wrapper around Exception (RAISE) ?

Class based exception are more powerful compared to the "legacy" exceptions. But you don't want to c.

Celebrating 5 years - 2008 to 2013

Abstract Class vs Interface – Which to use when?

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:

Recommendations

Based on the above mentioned differences, we can come to this recommendations:

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: