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 ).