Using Interfaces

Objectives

After completing this lesson, you will be able to:

  • Use Interfaces
  • Using Interfaces

Interface Use

Declaring an Interface Reference

You can use interfaces as a reference type for declaring a reference variable. As you learned earlier, you use reference variables to create and manage instances of a class. Since it is not possible to instantiate an interface, you may be wondering what kinds of objects you can manage using an interface reference.

Interface references may contain references to instances of any class that implements the interface in the same way that a reference variable with the type of a superclass can contain references to any subclasses. (If you now imagine that the superclass is abstract, you have an identical situation of a type that cannot be instantiated holding references to other classes that are related to it).

When you use a variable with the type of a superclass to manage an instance of one of its subclasses, you can only access the components that are defined in the superclass. Similiarly, when you use an interface reference to manage an instance of a class that implements the interface, you can only access the components that are defined in the interface. This means that the developer of a particular framework of classes can ship interfaces that provide a particular view of an object instead of exposing every single thing that the class can do.

You can call a method from an interface using a reference variable with the type of the class. In this case, you must use the fully-qualified name of the method (including the interface name). If you have defined an alias for the method, you can use that. When you call an interface method using an interface reference, the name of the interface is inferred from the type of the reference variable, and you do not need to specify the interface name before the method name.

Casting with Interface References

Assigning object references to an interface reference is also a cast; you look at the rental or carrier object as though it were a business partner. You can always assign an instance of the implementing class to an interface reference. This is an up-cast, and is guaranteed by the fact that the class implements the interface. You cannot, however, assign an object from an interface reference to a reference with the type of an implementing class, since the syntax check cannot tell whether the actual runtime objects will be compatible. The assignment go_rental = go_partner would work if the object in the interface reference really is an instance of lcl_rental, but if it is an instance of another implementing class, you would cause a runtime error.

Here we have a down-cast in the same way that we encountered it in inheritance. The solution here is the same. If you need to assign an object reference from a reference variable with the type of an interface to one with the type of a class (so that you can access specific features of the class), you must use the CAST operator. The correct solution in this case would be:

IF partner IS INSTANCE OF lcl_car_rental.

rental = CAST #( partner ).

ENDIF.

Try it Out: Interface Use

  1. Create a new class that implements the interface IF_OO_ADT_CLASSRUN.
  2. Switch to the Local Types tab and copy the following code snippet into the editor:
    Code snippet
    
    INTERFACE lif_partner.
    METHODS get_partner_attributes.
    ENDINTERFACE.
    
    
    CLASS lcl_travel_Agency DEFINITION.
    PUBLIC SECTION.
    
    ENDCLASS.
    
    
    CLASS lcl_airline DEFINITION.
    PUBLIC SECTION.
    
    INTERFACES lif_Partner.
    
    
    TYPES: BEGIN OF ts_detail,
    name TYPE string,
    value TYPE string,
    END OF ts_detail,
    tt_Details TYPE SORTED TABLE OF ts_detail WITH UNIQUE KEY name.
    
    
    METHODS get_details RETURNING VALUE(rt_details) TYPE tt_details.
    ENDCLASS.
    
    
    CLASS lcl_car_Rental DEFINITION.
    PUBLIC SECTION.
    interfaces lif_Partner.
    TYPES: BEGIN OF ts_info,
    name TYPE c LENGTH 20,
    value TYPE c LENGTH 20,
    END OF ts_info,
    tt_Info TYPE SORTED TABLE OF ts_info WITH UNIQUE KEY name.
    
    
    METHODS get_information RETURNING VALUE(rt_details) TYPE tt_info.
    ENDCLASS.
    
    
    CLASS lcl_airline IMPLEMENTATION.
    
    
    METHOD get_details.
    
    
    ENDMETHOD.
    
    
    METHOD lif_partner~get_partner_attributes.
    
    ENDMETHOD.
    
    ENDCLASS.
    
    
    CLASS lcl_car_rental IMPLEMENTATION.
    
    
    METHOD get_information.
    
    
    ENDMETHOD.
    
    
    METHOD lif_partner~get_partner_attributes.
    
    
    ENDMETHOD.
    
    
    ENDCLASS.
    Expand
  3. Switch to the Global Class tab and paste the following code snippet into the implementation of method if_oo_adt_classrun~main( ).
    Code snippet
    
    CLASS zcl_s4d401_interfaces_2 DEFINITION
    PUBLIC
    FINAL
    CREATE PUBLIC .
    
    
    PUBLIC SECTION.
    
    
    INTERFACES if_oo_adt_classrun .
    PROTECTED SECTION.
    PRIVATE SECTION.
    ENDCLASS.
    
    
    
    CLASS zcl_s4d401_interfaces_2 IMPLEMENTATION.
    
    METHOD if_oo_adt_classrun~main.
    
    
    DATA car_rental TYPE REF TO lcl_car_rental.
    DATA airline TYPE REF TO lcl_airline.
    DATA agency TYPE REF TO lcl_travel_agency.
    DATA partners TYPE TABLE OF REF TO lif_partner.
    
    agency = NEW #( ).
    car_rental = NEW #( iv_name = 'ABAP Autos' iv_contact_person = 'Mr Jones' iv_has_hgv = abap_true ).
    
    
    agency->add_partner( car_rental ).
    
    
    airline = NEW #( iv_name = 'Fly Happy' iv_contact_person = 'Ms Meyer' iv_city = 'Frankfurt' ).
    agency->add_partner( airline ).
    
    
    
    
    LOOP AT agency->get_partners( ) INTO DATA(partner).
    
    out->write( partner->get_partner_attributes( ) ).
    
    ENDLOOP.
    
    
    ENDMETHOD.
    ENDCLASS.
    Expand

Log in to track your progress & complete quizzes