ABAP Objects – Achieve Multiple Inheritance using Interfaces

By | May 1, 2012 | ABAP Objects, OO Concepts | 36,436 | 8

ABAP Objects doesn’t support Multiple Inheritance – Inheriting from more than one Super class, but similar functionality can be achieved using the interfaces.

What is Multiple Inheritance

Some languages support a feature in which a class can inherit components – methods, attributes, events – from more than one Superclass. In other words, you can define a class which has multiple “parent” class. Language like C++ supports this feature.

Multiple Inheritance Classes

ABAP objects doesn’t support multiple inheritance using more than one class. In ABAP Objects, you can only define a class inheriting from only one class. If you try to define multiple super class while defining a class, compiler would prompt an error message.

ABAP syntax error

How interfaces can help

Interface is an abstract type which can’t have implementation. It can only have method declarations, attributes and events. Interface can have multiple interface within it. You may want to check out Interface vs Abstract class to gain more understanding of Interfaces.

Since we can include more than one interface while declaring a class using keyword INTERFACES, you can achieve the multiple inheritance. The class which implements those interfaces, would have all the components available from all the interfaces. You must implement all the methods which are available from interfaces in the concrete class.

Multiple Class inheritance

Standard SAP has used multiple inheritance heavily in few development. E.g. Purchase Order Enjoy transaction. Like Purchase Order header class contain quite a lot interfaces within it to make it a full object. This way this object gets all the components from so many different interfaces. These interfaces could be also used to represent different “part” of different objects.

Demo UML

Here is the UML for this Demo

Multiple Inheritance Demo UML in ABAP

Code lines

Code to achieve multiple inheritance using interfaces.

 
REPORT  znp_multiple_inheritance.
 
*
INTERFACE lif_data.
  METHODS: get_data.
ENDINTERFACE.                    "lif_data
*
INTERFACE lif_output.
  METHODS: generate_output.
ENDINTERFACE.                    "lif_output
*
CLASS lcl_summary DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_data,
                lif_output.
ENDCLASS.                    "lcl_summary DEFINITION
*
CLASS lcl_summary IMPLEMENTATION.
  METHOD lif_data~get_data.
    WRITE: / 'Getting data for summary'.
  ENDMETHOD.                    "lif_data~get_Data
  METHOD lif_output~generate_output.
    WRITE: / 'Generating output'.
  ENDMETHOD.                    "lif_output~generate_output
ENDCLASS.                    "lcl_summary IMPLEMENTATION
*
CLASS lcl_detail_to_email DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_data,
                lif_output.
ENDCLASS.                    "lcl_detail_to_email DEFINITION
*
CLASS lcl_detail_to_email IMPLEMENTATION.
  METHOD lif_data~get_data.
    WRITE: / 'Getting data for detail'.
  ENDMETHOD.                    "lif_data~get_data
  METHOD lif_output~generate_output.
    WRITE: / 'Sending an email'.
  ENDMETHOD.                    "lif_output~generate_output
ENDCLASS.                    "lcl_detail_to_email IMPLEMENTATION
 
class lcl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: select_Data IMPORTING io_Data type ref to lif_data,
                   write_Data  IMPORTING io_output type ref to lif_output.
ENDCLASS.
*
class lcl_main IMPLEMENTATION.
  method select_Data.
    io_data->get_Data( ).
  ENDMETHOD.
  method write_Data.
    io_output->generate_output( ).
  ENDMETHOD.
ENDCLASS.
 
*
START-OF-SELECTION.
  datA: lo_report type ref to lcl_summary.
  create object lo_report.
  lcl_main=>select_Data( lo_report ).
  lcl_main=>write_Data( lo_report ).
 
  data: lo_dtl_rep type ref to lcl_detail_to_email.
  create OBJECT lo_dtl_rep.
  lcl_main=>select_Data( lo_dtl_rep ).
  lcl_main=>write_Data( lo_dtl_rep ).
 

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

8 Comments

  • Shariq

    Thanks Naimesh for posting this concept…I have noticed comments at many places saying that multiiple inheritance is not possible using OOPS ABAP.

    Just to add this concept is the basis for Cross component application development used in WDA where in components of a WD application can access / call class components of other WD’s through interfacing.

  • Hello Shariq,

    Yes you are correct WDA framework also uses multiple inheritance to more re-usability and abstraction.

    Regards,
    Naimesh Patel

  • Yellappa

    Hi Naimesh,

    I like your blogs very much. Your explanation is very simple to understand. I have a one suggestion in UML notation. In UML diagram interfaces are used in two ways. LCL_MAIN uses them where as LCL_SUMMARY and LCL_DETAIL_TO_EMAIL implements them. But the notation is same. You have used text to indicates whether interface is used or implemented. But in UML, we mention the implementation of interface with closed empty triangle and dashed line.

    Regards,
    Yellappa.

  • Hello Yellappa,

    I’m glad that you enjoy reading the post here.

    I try to keep UML simple thus differentiate the interface usage versus implementation using the text. In future, I would try to adhere to UML standards in my UML design. I appreciate your feedback.

    Regards,
    Naimesh Patel

  • Timo John

    Hi, thanks for writing nice posts on ABAP OO.

    Personally, I do not see this concept as “Multiple Inheritance”, but I like the usage of interfaces…
    I think this is more some other pattern like delegation or something like this.

    Redefinition of methods is not possible using this technique, cause there is no inheritance defined.

    Anyway this is a nice solution to use different object for the same actions making them similar using interfaces, but that is exactly THAT what they are used to be used for… in my opinion…

  • Hello Timo,

    I agree that redefinition is not possible when Interfaces are used. But you can surely achieve more abstraction and polymorphism. You can declare the object with Interface reference and at runtime, switch it with concrete implementation. This could be any implementation. As long as it has the interface implemented, its all good. I guess, you can consider this as Multiple Inheritance.

    Thanks,
    Naimesh Patel

  • Namit Narayanan

    Very simple and very straight.I loved your blog and its helping me a lot to understand and clearing my doubts in OOPS.

    Thanks a lot for helping us with such blog.Hoping to have more concepts in simpler manner.

  • Hello Namit,

    Glad you like it. There are quite a few article already about OO ABAP. I would try to add as many as I could. I hope you are already Subscribed to the blog.

    Regards,
    Naimesh Patel

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.