Você está na página 1de 4

ABAP Objects Achieve Multiple Inheritance using 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.

ABAP objects doesnt 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.

How interfaces can help

Interface is an abstract type which cant 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.

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

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

Você também pode gostar