Previous | Table of Contents | Next |
Exercise 13.11
Modify ImprovedFibonacci to store the String objects it creates into an array instead of invoking println with them directly.
One of the major benefits of object orientation is the ability to extend, or subclass, the behavior of an existing class and continue to use code written for the original class.
When you extend a class to create a new class, the new extended class inherits all the fields and methods of the class that was extended. The original class on which the extension is based is known as the superclass.
If the subclass does not specifically override the behavior of the superclass, the subclass inherits all the behavior of its superclass, because, as we said, the extended class inherits the fields and methods of its superclass.
The Walkman example can itself be extended in this way. Later models incorporated two sets of jacks so two people could listen to the same tape. In the object-oriented world, the two-jack model extends, or is a subclass of, the basic model. The two-jack model inherits the characteristics and behavior of the basic model and adds new behavior of its own.
Customers told Sony they wanted to talk to each other while sharing a tape in the two-jack model. Sony enhanced the two-jack model to include two-way communications so people could chat while listening to music. The two-way communications model is a subclass of the two-jack model, inherits all of its behavior, and adds new behavior.
Sony created many other Walkman models. Later models extend the capabilities of the basic modelthey subclass the basic model and inherit features and behavior from it.
Lets look at an example of extending a Java class. Here we extend our former Point class to represent a screen pixel. The new Pixel class requires a color in addition to x and y coordinates:
class Pixel extends Point { Color color; public void clear() { super.clear(); color = null; } }
Pixel extends both data and behavior of its Point superclass. Pixel extends the data by adding an additional field, namely color, to Point. Pixel also extends the behavior of Point by overriding Points clear method. Here is an illustration of the concept:
Pixel objects can be used by any code designed to work with Point objects. If a method expects a parameter of type Point, you can hand it a Pixel object and it just works. All the Point code can be used by anyone with a Pixel in hand. This feature is known as polymorphism: A single object like Pixel can have many (poly-) forms (-morph) and can be used as both a Pixel object and a Point object.
Pixels behavior extends Points behavior. Extended behavior could be entirely new (adding color in this example) or can be a restriction on old behavior that follows all the original requirements. An example of restricted behavior might be Pixel objects that live inside some kind of Screen object, restricting x and y to the dimensions of the screen. The original Point class made no restriction on coordinates, but a class with restricted range is still within the original unbounded range.
An extended class often overrides the behavior of its superclass (the class that was extended) by providing new implementations of one or more of the inherited methods. In the example above, we override clear to obtain proper behavior that Pixel requiresthe clear that Pixel inherited from Point knows only about Points fields, but obviously cant know about the new color field declared in the Pixel subclass.
Exercise 13.12
Write a set of classes that reflect the class structure of the Sony Walkman product family. Use methods to hide the data, making all the data private and the methods public. What methods would belong in the base Walkman class? Which methods would be added for which extended classes?
Classes that do not explicitly extend any other class implicitly extend the Object class. All object references are polymorphically of class Object, so Object is the generic class for references that can refer to objects of any class:
Object oref = new Pixel(); oref = Some String;
In this example, oref is legally assigned references to Pixel and String objects, even though those classes have no relationship other than the direct or indirect implicit superclass of Object.
Previous | Table of Contents | Next |