0% found this document useful (0 votes)
4 views3 pages

Interface Example Multiple Inheritance

The document defines two interfaces, if_printable and if_exportable, and implements them in two classes: lcl_invoice and lcl_delivery_note. The lcl_invoice class supports both printing and exporting functionalities, while the lcl_delivery_note class only supports printing. The program demonstrates object creation, method calls, and upcasting/downcasting between interfaces and classes.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Interface Example Multiple Inheritance

The document defines two interfaces, if_printable and if_exportable, and implements them in two classes: lcl_invoice and lcl_delivery_note. The lcl_invoice class supports both printing and exporting functionalities, while the lcl_delivery_note class only supports printing. The program demonstrates object creation, method calls, and upcasting/downcasting between interfaces and classes.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

*&---------------------------------------------------------------------*

*& Report ZABSTRACT


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zabstract.

"-------------------------------------------------------
" Interface: Printable
"-------------------------------------------------------
INTERFACE if_printable.

CONSTANTS:
c_format TYPE string VALUE 'PDF'.

METHODS:
print,
get_document_type RETURNING VALUE(rv_doc_type) TYPE string.

CLASS-METHODS:
show_print_info.

ENDINTERFACE.

"-------------------------------------------------------
" Interface: Exportable (another capability)
"-------------------------------------------------------
INTERFACE if_exportable.

METHODS:
export_as_json,
export_as_xml.

ENDINTERFACE.

"-------------------------------------------------------
" Class: Invoice - Implements 2 interfaces
"-------------------------------------------------------
CLASS lcl_invoice DEFINITION.
PUBLIC SECTION.
INTERFACES: if_printable, if_exportable.
METHODS: get_total_amount,
display_invoice_id.

ENDCLASS.

CLASS lcl_invoice IMPLEMENTATION.

METHOD if_printable~print.
WRITE: / 'Printing Invoice as:', if_printable=>c_format.
ENDMETHOD.

METHOD if_printable~get_document_type.
rv_doc_type = 'INVOICE'.
ENDMETHOD.

METHOD if_exportable~export_as_json.
WRITE: / 'Invoice exported in JSON format'.
ENDMETHOD.

METHOD if_exportable~export_as_xml.
WRITE: / 'Invoice exported in XML format'.
ENDMETHOD.

METHOD get_total_amount.
WRITE: / 'Total Invoice Amount: 2500.00'.
ENDMETHOD.

METHOD display_invoice_id.
WRITE: / 'Invoice ID: INV1001'.
ENDMETHOD.

METHOD if_printable~show_print_info.
WRITE: / 'This document will be printed in', if_printable=>c_format, 'format.'.
ENDMETHOD.

ENDCLASS.

"-------------------------------------------------------
" Class: Delivery Note - Implements only Printable
"-------------------------------------------------------
CLASS lcl_delivery_note DEFINITION.
PUBLIC SECTION.
INTERFACES: if_printable.
METHODS: delivery_partner.
ENDCLASS.

CLASS lcl_delivery_note IMPLEMENTATION.

METHOD if_printable~print.
WRITE: / 'Printing Delivery Note as:', if_printable=>c_format.
ENDMETHOD.

METHOD if_printable~get_document_type.
rv_doc_type = 'DELIVERY NOTE'.
ENDMETHOD.

METHOD delivery_partner.
WRITE: / 'Delivered via: Speed Logistics'.
ENDMETHOD.

METHOD if_printable~show_print_info.
WRITE: / 'This document will be printed in', if_printable=>c_format, 'format.'.
ENDMETHOD.

ENDCLASS.

"-------------------------------------------------------
" Program Execution
"-------------------------------------------------------

START-OF-SELECTION.

DATA: lo_doc_printable TYPE REF TO if_printable,


lo_exportable TYPE REF TO if_exportable,
lo_invoice_class TYPE REF TO lcl_invoice,
lo_delivery TYPE REF TO lcl_delivery_note.
"✅ Create object and demonstrate UPCASTING
CREATE OBJECT lo_invoice_class.

CREATE OBJECT lo_delivery.

lo_doc_printable = lo_invoice_class. " Upcasting to if_printable


lo_exportable = lo_invoice_class. " Upcasting to if_exportable

"✅ Call interface methods via interface reference


lo_doc_printable->print( ).
WRITE: / 'Doc Type:', lo_doc_printable->get_document_type( ).

lo_exportable->export_as_json( ).
lo_exportable->export_as_xml( ).

"✅ Call static method via class


lcl_invoice=>if_printable~show_print_info( ).

ULINE.

"✅ Call subclass-specific method using object reference


lo_invoice_class->display_invoice_id( ).
lo_invoice_class->get_total_amount( ).

ULINE.

"✅ Narrow Casting from interface back to class


DATA(lo_casted_invoice) = CAST lcl_invoice( lo_doc_printable ).
lo_casted_invoice->get_total_amount( ).

ULINE.

"✅ Dynamic Method Call


CALL METHOD lo_doc_printable->('PRINT').

"✅ Another object (Delivery Note)


DATA(lo_delivery_1) = NEW lcl_delivery_note( ).
DATA(lo_delivery_interface) = lo_delivery_1.

lo_delivery_interface->if_printable~print( ).
WRITE: / 'Doc Type:', lo_delivery_interface->if_printable~get_document_type( ).

" Narrow cast to call subclass-only method


DATA(lo_casted_delivery) = CAST lcl_delivery_note( lo_delivery_interface ).
lo_casted_delivery->delivery_partner( ).

" Dynamic call


CALL METHOD lo_delivery_interface->('PRINT').

You might also like