Pixel's behavior extends Point's behavior. Extended behavior can 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. If the original Point class did not forbid restrictions for coordinates, a class
with restricted range would not violate the original class's behavior.
An extended class often overrides the behavior of its superclass by providing new implementations of one or
more of the inherited methods. To do this the extended class defines a method with the same signature and
return type as a method in the superclass. In the Pixel example, we override clear to obtain the proper
behavior that Pixel requires. The clear that Pixel inherited from Point knows only about Point's
fields but obviously can't know about the new color field declared in the Pixel subclass.
1.11.1. Invoking Methods of the Superclass
To make Pixel do the correct "clear" behavior, we provide a new implementation of clear that first
invokes its superclass's 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
superclassnamely, Point. After invoking super.clear() to clear out the Point part of the object, we
add new functionality to set color to a reasonable empty value. We choose null, a reference to no object.
What would happen had we not invoked super.clear() in the previous example? Pixel's clear
method would set 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 runtime system looks back up the inheritance hierarchy to the
first superclass that contains the required method. If Point didn't have a clear method, for example, the
runtime system would look at Point's superclass for such a method and invoke that, and so on.
For all other references, invoking a method uses the actual class of the object, not the type of the object
reference. Here is an example:
Point point = new Pixel();
point.clear(); // uses Pixel's clear()
In this example, Pixel's version of clear is invoked even though the variable that holds the Pixel is
declared as a Point reference. But if we invoke super.clear() inside one of Pixel's methods, that
invocation will use the Point class's implementation of the clear method.
1.11.2. The Object Class
Classes that do not explicitly extend any other class implicitly extend the Object class. All objects are
polymorphically of class Object, so Object is the general-purpose type for references that can refer to
objects of any class:
Object oref = new Pixel();
oref = "Some String";