Previous Table of Contents Next


14.3.6. Subclasses and Inheritance

The Circle class we’ve defined is good for abstract mathematical manipulation. For some applications this is exactly what we need. For other applications, we want to be able to manipulate circles and draw them on the screen. This means we need a new class, GraphicCircle, that has all the functionality of Circle, but also has the ability to be drawn.

We want to implement GraphicCircle so that it can make use of the code we’ve already written for Circle. One way to do that is the following:

   public class GraphicCircle {
       // Here is the mathematical circle
       public Circle c;
       // Here are the old methods
       public double area() { return c.area(); }
       public double circumference() { return c.circumference(); }

       // The new graphic variables and methods go here
       public Color outline, fill;
       public void draw(DrawWindow dw) { /* code omitted */ }
   }

This approach would work, but it is not particularly elegant. The problem is that we have to write stubs for methods like area() and circumference() that have nothing to do with drawing circles. It would be nice if we didn’t have to do this.

14.3.6.1. Extending a Class

In fact, we don’t have to do it this way. Java allows us to define GraphicCircle as an extension, or subclass of Circle. Example 14.16 shows how. Note that this example assumes we have two other classes of objects defined: Color, which represents a color, and DrawWindow, a class that has the window into which drawing is done and that defines the primitive methods to do the drawing.

EXAMPLE 14.16. Subclassing a class.

   public class GraphicCircle extends Circle {
       // We automatically inherit the variables and methods of
       // Circle, so we only have to put the new stuff here.
       // We’ve omitted the GraphicCircle constructor, for now.
       Color outline, fill;
       public void draw(DrawWindow dw) {
           dw.drawCircle(x, y, r, outline, fill);
       }
   }

The extends keyword tells Java that GraphicCircle is a subclass of Circle, and that it inherits the fields and methods of that class.19 The definition of the draw() method shows variable inheritance—this method uses the Circle variables x, y, and r as if they were defined right in GraphicCircle itself.


19Except for private fields and methods. We’ll discuss private members of a class later. C++ programmers note that extends is the Java equivalent of the : operator in C++—both indicate the superclass of a class.

GraphicCircle also inherits the methods of Circle. Thus, if we have a GraphicCircle object referred to by variable gc, we can say:

   double area = gc.area();

This works just as if the area() method were defined in GraphicCircle itself.

Another feature of subclassing is that every GraphicCircle object is also a perfectly legal Circle object. Thus, if gc refers to a GraphicCircle object, we can assign it to a Circle variable, and we can forget all about its extra graphic capabilities: Circle c = gc;.

14.3.6.2. Final Classes

When a class is declared with the final modifier, it means that it cannot be extended or subclassed. java.lang.System is an example of a final class. Declaring a class final prevents unwanted extensions to the class. But it also allows the compiler to make some optimizations when invoking the methods of a class. We’ll explore this in more detail when we talk about method overriding later in this section.

14.3.6.3. Superclasses, Object, and the Class Hierarchy

In our example, GraphicCircle is a subclass of Circle. We can also say that Circle is the superclass of GraphicCircle. The superclass of a class is specified in its extends clause:

   public class GraphicCircle extends Circle { ... }

Every class you define has a superclass. If you do not specify the superclass with an extends clause, the superclass is the class Object. Object is a special class for a couple of reasons:

  It is the only class that does not have a superclass.
  The methods defined by Object can be called by any Java object.

Because every class has a superclass, classes in Java form a class hierarchy, which can be represented as a tree with Object at its root. Figure 14.3 shows a class hierarchy diagram which includes our Circle and GraphicCircle classes, as well as some of the standard classes from the Java API.


FIGURE 14.3.  A class hierarchy diagram.


Previous Table of Contents Next