In our earlier logging example, we learned that a class can inherit from another class while also implementing an interface. Instances of the subclass belong to both the superclass's datatype and the interface's datatype. For example, instances of the earlier LogUI class belonged to both the Sprite and LogRecipient datatypes because LogUI inherited from Sprite and implemented LogRecipient. Let's take a closer look at this important architectural structure with a new example.
The following discussion requires a prior knowledge of arrays (ordered lists of values), which we haven't covered yet. If you are new to arrays, you should skip this section for now and return to it after you have read Chapter 11.
Suppose we're creating an application that stores objects on a server via a server-side script. Each stored object's class is responsible for providing a method, serialize( ), that can return a string-representation of its instances. The string representation is used to reconstitute a given object from scratch.
One of the classes in the application is a simple Rectangle class with
lineColor instance variables. To represent
Rectangle objects as strings, the
Rectangle class implements a
serialize( ) method that returns
a string of the following format:
To store a given Rectangle object on the server, we invoke serialize( ) on the object and send the resulting string to ...