Mentor SAP

Polymorphism with Interfaces

An interface reference can only refer to instances of classes that have implemented the interface because interfaces themselves cannot be instantiated. As in regular inheritance, you must use upcast to copy a reference to the interface reference variable to perform polymorphism with interfaces.

 

If the class has implemented the interface, it is certain that all the components that can be accessed syntactically after the cast assignment are available in the instance. A user can address the instance of the implementing class using the interface. The prefixing of the interface name and the interface resolution operator is omitted. However, the user is restricted to using the components from the interface.

 

In the example, the DISPLAY_PARTNER and CHECK_AVAILABILITY methods of the LIF_PARTNER interface can be accessed only after assigning the reference variable GO_PARTNER. It is not possible to access the specific components of the instance from the LCL_RENTAL (GET_NAME in the example) class using the GO_PARTNER reference variable.

 

Therefore, the view is narrowed or, at least, remains unchanged. That is why you describe this type of assignment of reference variables as an upcast. There is a switch from a view of several components to a view of only a few components. The target reference can, of course, accept more dynamic types after the assignment, than it could before. Therefore, the term widening cast is also suitable.

 

 

Generic Access with Interfaces

A typical area of use for upcast assignments in preparation for generic access. A user who is not interested in the finer points of the instances of the class that implement the interface but who simply wants to address the components defined in the interface could use an interface reference for this access.

 

In the example shown, a travel agency (LCL_TRAVEL_AGENCY) needs to manage the various kinds of business partners in one list. The developer must, therefore, assign the row type of the internal table as the reference to the interface LIF_PARTNER.

 

The travel agency only wants to request the services in order to display its attributes and check their availability. The relevant DISPLAY_PARTNER and CHECK_AVAILABILITY methods are defined in the LIF_PARTNER interface and implemented in all business partner classes.

 

The objects of different classes (LCL_HOTEL, LCL_RENTAL, and LCL_CARRIER in the example) can be kept in an internal table, typed with interface references (LIF_PARTNER in the example). The components defined in the interface can then be accessed uniformly.

 

For this example, the ADD_PARTNER method is needed. This method copies the references to all kinds of business partners in the internal table. The import parameter of this method is already typed as the reference to the interface.

 

 

Generic Access Using the Interface Reference

Polymorphism can also be performed for interfaces. You can use interface references to call methods and execute different implementations depending on the object of the reference.

 

The dynamic type of the reference variable is used to search for the implementation of a method. In the above example, lo_partner->display_partner( ) uses the class of the instance to which lo_partner actually refers in order to search for the implementation of display_partner.

 

The implementation that is executed when DISPLAY_PARTNER is called now depends on the object to which the interface reference lo_partner currently refers. When objects from different classes react differently to the same method calls, it is known as polymorphism.

 

The option of performing polymorphism is one of the main strengths of interfaces. A client can handle different classes uniformly, regardless of their implementation. The runtime system searches for the right implementation of a method on behalf of the client.

 

Polymorphism can be used to write programs that are highly generic, that is, they do not need to be changed significantly if use cases are added.

 

In the example, it becomes easy to add a class for boat rentals. For example, the relevant class with the name LCL_SHIPPING will simply have to implement the LIF_PARTNER interface and the DISPLAY_PARTNER method defined there. Business partner management can then easily include ship-owning companies and request them to display their attributes.