Interfaces
The dynamic type information associated with an object reference variable allows
the ABAP runtime environment to dynamically bind a method call with the
implementation defined in the object pointed to by the object reference variable.
For example, the importing parameter im_animal for method play in the
lcl_see_and_say class from Listing 6.6 refers to an abstract type that could never
be instantiated on its own. However, whenever the method is called with a con-
crete subclass implementation such as lcl_cat or lcl_dog, the dynamic type of
the im_animai reference parameter is bound to one of these concrete types.
Therefore, the calls to methods get_type and speak refer to the implementations
provided in the lcl_cat or lcl_dog subclasses rather than the undefined abstract
implementations provided in class lcl_animal.
Dynamic binding provides for tremendous flexibility in designs. The simple
example from Listing 6.6 only implemented for a cat and a dog. In the future,
developers may decide to implement subclasses for various other types of
animals such as a horse, a cow, a pig, and so on. However, because the
lcl_see_and_say class works with the generic lcl_animal type, they can inte-
grate these new types into the "See-n-Say device" seamlessly. Such designs are
said to be extensible in the sense that we can easily introduce new functionality by
simply creating a new subclass and plugging it into the design.
6.3 Interfaces
Throughout the course of this book, we have used the term interface to describe
various interaction points between classes and their clients. For example, a
method's signature defines an interface that is used by clients wanting to call that
method. From an object-oriented perspective, you can think of an interface as a
type of protocol that defines rules for communicating with objects.
This analogy should be familiar because we interact with many types of protocols
every day. For instance, the Hypertext Transfer Protocol (HTTP) defines the rules
that clients (i.e., web browsers such as Microsoft Internet Explorer) and web serv-
ers must adhere to in order to reliably publish and retrieve content on the World
Wide Web. These rules make it possible for your web browser to request web
pages from many different types of web server implementations (e.g., Microsoft,
Apache, SAP, etc.) without having to worry about how these servers are imple-
mented. Similarly, you have seen how polymorphism can be used to dynamically
bind many different types of implementations to a single interface.
163