Previous | Table of Contents | Next |
To make Pixel do the correct clear behavior, we provide a new implementation of clear that first invokes its superclasss clear using the super reference. The super reference is a lot like the this reference described previously, except that super references things from the superclass, whereas this references things from the current object.
The invocation super.clear() looks to the superclass to execute clear as it would for an object of the superclass typenamely, Point. After invoking super.clear(), we add new functionality, to set color to a reasonable empty value. We choose nulla reference to no object.
What would have happened had we not invoked super.clear()? Pixels clear method would set the color to its null value, but the x and y variables that Pixel inherited from Point would not be set to any cleared values. Not clearing all the values of a Pixel object, including its Point parts, is probably a bug.
When you invoke super.method(), the object runtime system looks back up the inheritance hierarchy to the first superclass that contains the required method(). If Point didnt have a clear method, for example, the object runtime would look at Points superclass for such a method, and invoke that, and so on.
For all other references you use, invoking a method uses the actual type of the object, not the type of the object reference. Here is an example:
Point point = new Pixel(); point.clear(); // uses Pixels clear ()
In this example, Pixels version of clear is invoked, even though the variable that holds the Pixel is declared as a Point reference. But if we write super.clear() inside one of Pixels methods, Points clear method is invoked.
Sometimes you need only define methods an object must support, but not necessarily supply the implementation of those methods. As long as their behavior meets specific criteria, implementation details of the methods are irrelevant. For example, to ask whether a particular value is contained in a set of values, details of how those values are stored are irrelevant. You would want the methods to work equally well with a linked list of values, a hashtable of values, or any other data structure.
Java enables you to define an interface, which is a like a class, but with only declarations of its methods. The designer of the interface declares which methods are supported by classes that implement the interface, and what those methods should do. Here is a Lookup interface:
interface Lookup { /** Return the value associated with the name, or * null if there is no such value */ Object find(String name); }
The Lookup interface defines one method, find, that takes a String and returns the value associated with that name, or null if there is no associated value. No implementation is given for the methodthe class that implements the interface is responsible for the specific implementation. Code that uses references to Lookup objects (objects that implement the Lookup interface) can invoke the find method and get the expected results, no matter the actual type of the object:
void processValues(String[] names, Lookup table) { for (int I = 0; I < names.length; i++) { Object value = table.find(names[i]); if (value != null) processValue(names[i], value); } }
A class can implement as many interfaces as it chooses. This example implements Lookup using a simple array (methods to set or remove values are left out for simplicity):
class SimpleLookup implements lookup { private String[] Names; private Object[] Values; public Object find(String name) { for (int I = 0; I < Names.length; I++) { if (Names[i].equals(name)) return Values[i]; } return null; // not found } // ... }
Interfaces can be extended, too, using the extends keyword. An interface can extend one or more other interfaces, adding new constants and new methods that must be implemented by any class that implements the extended interface.
A classs supertypes are the class it extends and the interfaces it implements, including all the supertypes of those classes and interfaces. So the type of an object is not only its class, but any of its supertypes, including interfaces. An object can be used polymorphically with both its superclass and any superinterfaces, including any of their supertypes.
Exercise 13.13
Write an extended interface of Lookup that has add and remove methods. Implement the extended interface in a new class.
Previous | Table of Contents | Next |