Power of Inheritance

Without knowing it, you have used inheritance every time you worked with one of the standard Java classes such as String or Math. Java classes are organized into a pyramid-shaped hierarchy of classes in which all classes descend from the Object class. A class of objects inherits from all superclasses that are above it. To get a working idea of how this operates, you'll look at the JApplet class. This class is a superclass of all Swing applets, Java programs written to run on the World Wide Web. The JApplet class, which is used to create Java 2 applets, is a subclass of Applet. A partial family tree of JApplet is shown in Screenshot. Each of the boxes is a class, and the lines connect a superclass to any subclasses below it.

Screenshot The family tree of the Applet class.

Java ScreenShot

At the top is the Object class. JApplet has five superclasses above it in the hier archy: Applet, Panel, Container, Component, and Object. The JApplet class in herits attributes and behavior from each of these classes because each is directly above it in the hierarchy of superclasses. JApplet does not inherit anything from the five shaded classes in Screenshot, which include Dialog and Frame, because they are not above it in the hierarchy. If this seems confusing, think of the hierarchy as a family tree. JApplet will inherit from its parent, the parent's parent, and on upward. It even might inherit some things from its great-great-grandparent, Object. The JApplet class won't inherit from its siblings or its cousins, however. Setting up a complicated hierarchy of classes is a difficult thing, but it makes it easier to create new programs later. The amount of work necessary to write a new class of objects is reduced. Creating a new class boils down to the following task: You only have to define the ways in which it is different from an existing class. The rest of the work is done for you. As an example, consider the popular video game Tetris. Since the game was invented by Soviet mathematician Alexey Pajitnov, it has been adapted for dozens of different operating systems and coding languages—including several different Java versions. In case you somehow avoided Tetris during the past decade by lapsing into a coma or falling into a deep meditative trance, the game works as follows: Blocks of different shapes fall from the top of the screen, and you must organize them into unbroken horizontal lines before they stack up too high. The Java source file for several adaptations of Tetris is available for your use. If you wanted to create a new version of Tetris based on one of these existing classes, you could make your game a subclass of an existing Tetris game. All you would have to do is create the things that are new or different about your version, and you would end up with a new game.

Inheriting Behavior and Attributes

The behavior and attributes of a class are a combination of two things: its own behavior and attributes, and all the behavior and attributes it inherits from its superclasses. The following are some of the behavior and attributes of Applet:

  • The equals() method determines whether a JApplet object has the same value as another object.
  • The setBackground() method sets the background color displayed on the applet window.
  • The add() method adds user interface components such as buttons and text fields to the applet.
  • The setLayout() method defines how the applet's graphical user interface will be organized.

The JApplet class can use all of these methods, even though setLayout() is the only one it didn't inherit from another class. The equals() method is defined in Object, setBackground() comes from Component, and add() comes from Container.

Overriding Methods

Some of the methods defined in the JApplet class of objects also were defined in one of its superclasses. As an example, the update() method is set up in both the JApplet class and the Component class. This method was originally used in Java 1.0 to control whether the applet window is cleared out before anything is drawn in it. In Java 2, the JApplet class has its own update() method that never clears the applet window. When a method is defined in a subclass and its superclass, the subclass method is used. This enables a subclass to change, replace, or completely wipe out some of the behavior or attributes of its superclasses. In the case of update(), the purpose was to wipe out some behavior present in a superclass. Creating a new method in a subclass to change behavior inherited from a superclass is called overriding the method. You need to override a method any time the inherited behavior will produce an undesired result.