OO ABAP Concepts INTERFACES POLYMORPHISM Interfaces The only
OO ABAP Concepts INTERFACES & POLYMORPHISM
Interfaces The only real difference between interfaces and inheritance is the role they play. The programming advantages are thus the same as for inheritance. Interfaces differ from regular inheritance in their area of use. In terms of programming, there are hardly any differences, however. From a technical point of view, interfaces are simply superclasses that: cannot be instantiated, do not have an implementation part, and only have public components
Interfaces In ABAP Objects, interfaces primarily serve to define uniform interfaces for services. Interfaces contain no implementations. In ABAP Objects, the same components can generally be defined in interfaces and in classes.
Use of Interfaces Some typical use cases of interfaces are: q You want to allow for the option of having multiple classes implementing a service in different ways, but using the same method names. • q With regular inheritance, you would define such a method in the shared superclass. However, if you cannot model a superclass suitably for inheritance, you need to define an interface and then define this method there. Classes implement interfaces as follows: • The interface name is listed in the definition part of the class with the INTERFACES statement. This must be in the PUBLIC SECTION, that is, interfaces can only be implemented publicly. • The interface methods must be implemented in the implementation part of the class. • The components defined in the interface can be addressed in the implementation part of the class.
Access to components Interface components are distinguished from the other components in the implementing class by prefixing the interface name followed by a: § For example: § ~ interface_name~component_name. To simplify accessing interface components you can work with alias names, for example: § ALIASES a_1 FOR lif_1~method_1. § The interface method lif_1~method_1 can then be addressed with the shorter form ref->a_1 Interface components can only be accessed by using an object reference, whose class implements the interface.
Practical Example Let’s define and implement some extra classes (besides the ones we already created in previous lessons) to illustrate a better example. Until now, we have: To understand interfaces, we are going to create: ZCL_VEHICLE ZCL_TRUCK ZCL_CAR ZCL_BUS ZCL_TRAVEL_AGENCY Public method: ADD_PARTNER Private attribute: PARTNER_LIST ZCL_RENTAL ZCL_HOTEL Public method: ADD_VEHICLE Private attributes: VEHICLE_LIST Public method: ADD_GUEST Private attributes: GUEST_LIST
Ø These classes can’t be modeled using inheritance. A hotel is not a subtype of a travel agency, and neither is a rental, but they have a partnership. Ø The Rental and the Hotel are the Travel Agency partners. Many hotels and rentals can be stored in attribute table PARTNER_LIST. Ø What type can PARTNER_LIST table have so it can store both, the rental and hotel objects? Ø It can have interface type ZIF_PARTNER. ZCL_RENTAL and ZCL_HOTEL implement ZIF_PARTNER. ZCL_TRAVEL_AGENCY uses it. ZCL_TRAVEL_AGENCY Public method: ADD_PARTNER Private attribute: PARTNER_LIST uses <<ZIF_PARTNER> > Public method: DISPLAY_PARTNE R Ø DATA: partner_list TYPE TABLE OF REF TO zif_partner Ø We use narrowing cast to store all the different hotels and rentals (partners) in PARTNER_LIST. implements ZCL_RENTAL ZCL_HOTEL Public method: ADD_VEHICLE Private attributes: VEHICLE_LIST Public method: ADD_GUEST Private attributes: GUEST_LIST
Practical Example OPEN TRANSACTION SE 24…
Polymorphism When objects from different classes react differently to the same method calls, this is known as polymorphism. Polymorphism can be performed for interfaces: Interface references can be used to call methods, whereby different implementations can be executed depending on the object of the reference. If you want to perform polymorphism with interfaces, you must use narrowing cast to copy a reference to the reference variable that was typed with the interface. The dynamic type and not the static type of the reference variable is used to search for the implementation of a method.
ZCL_TRAVEL_AGENCY Example Class Public methods: ADD_PARTNER DISPLAY_AGENCY_PARTNER S Private attribute: PARTNER_LIST METHOD display_agency_partners. DATA partner TYPE REF TO zif_partner. LOOP AT partner_list INTO partner->display_partner( ). ENDLOOP. ENDMETHOD. Interface uses ZIF_PARTNER Public method: DISPLAY_PARTNE R Open Transaction SE 24 uses partners react differently to the same method call Polymorphism Classes ZCL_RENTAL Public methods: DISPLAY_PARTNER DISPLAY_RENTAL_DAT A Private attributes: VEHICLE_LIST Implement methods METHOD zif_partner~display_partner. display_rental_data( ). ENDMETHOD. ZCL_HOTEL Public methods: DISPLAY_PARTNER DISPLAY_HOTEL_ATTRI BUTES Private attributes: GUEST_LIST METHOD zif_partner~display_partner. display_hotel_attributes( ). ENDMETHOD.
Source Code
- Slides: 11