Defining Interfaces

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Define interfaces

Interface Definition

Reasons to Use Interfaces

Interfaces allow you to define how a program will address one or more classes. In an interface, you can define methods, attributes, types, and constants, just as you would in a class. These components form a description of how a client program could interact with the corresponding objects. However, an interface cannot be instantiated, and does not contain any implementations of its methods. To make the concept work, we also need one or more classes that implement the interface. This means that they include the interface in their own class declaration and the components that are declared in the interface become part of the class itself. If the interface contains method declarations, the class that implements the interface must also provide an implementation of that method.

Interfaces have two important uses:

Providing a unified approach to different classes: If you have classes that are not related by inheritance, but that should still provide common services to their users, you can define the common services in an interface. This ensures that each class provides a method with the same name and the same signature to perform a particular task. Programs that use the classes then have a single way to call particular functions regardless of which class they are actually addressing.

Providing a simplified approach to a complex class: As you will see later on, interfaces provide a restricted view of an object. This enables you to expose a particular set of methods to a program that will use a particular class on the basis of the functions that the program actually requires.

Watch this example to know how an interface works.

Interface Definition

The definition of an interface is very similar to the definition section of a class. However, since an interface does not have an implementation, there is no DEFINITION addition in the INTERFACE statement. The second major difference is that there are no visibility sections in an interface, as all components of an interface are public. Interfaces are useful because of their interaction with other classes, so having non-public components would make no sense.

In the interface definition, you can use types, attributes, constants, and methods, and both instance and static components are allowed. Remember that interface methods have a definition, but no implementation. This will be supplied by the implementing class.

In many ways, interfaces are similar to abstract superclasses. They provide a useful technique for cases in which you need to define common components for classes without there being a case for inheritance, in other words, there is no clear "is a" relationship.

Declaring the Interface

To implement an interface in a class, you use the INTERFACES statement. As all components of the interface are public and you cannot change the visibility of an existing component, you must include the interface in the public section of the class.

As soon as you have written the INTERFACES statement, your program is syntactically incorrect. This is because the interface contains a method that you have not yet implemented. You can use a quick fix (Ctrl + 1) to add the method implementation to your class.

When you declare in interface in a class, you must implement all of its methods.

In the implementation of your class, you must implement all of the methods that are declared in the interface. When you name the method in the implementation, you must use the fully-qualified name, which consists of the interface name and the method name joined by a tilde. This is because there is no guarantee that the method name by itself is unambiguous; remember that component names must only be unique in the class or interface in which they are declared. Consequently, a class could declare a method called get_partner_attributes as well as the interface lif_partner.

Try It Out: Interface Definition

  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. Press Ctrl + F3 to activate the class.
  4. Familiarize yourself with the code.
    Note
    There is no executable code in this example.

Log in to track your progress & complete quizzes