The Power of Inheritance

You have used inheritance every time you worked with one of the standard Java classes such as String or Integer. 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, consider the JApplet class. This class is a superclass of all applets, browser-based Java programs that use a graphical user interface framework called Swing. The JApplet class is a subclass of Applet.

A partial family tree of JApplet is shown in Figure 12.1. Each of the boxes is a class, and the lines connect a superclass to any subclasses below it.

Figure 12.1. The family tree of the JApplet class.

Image

At the top is the Object class. JApplet has five superclasses above it in the hierarchy: Applet, Panel, Container, Component, and Object.

The JApplet class inherits 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 green classes in Figure 12.1, 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 inherits from its parent, the parent’s parent, and on upward. It even might inherit some things from its great-great-great-grandparent, Object. The JApplet class doesn’t inherit from its siblings or its cousins, however.

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.

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 JApplet:

• The equals() method determines whether a JApplet object has the same value as another object.

• The setBackground() method sets the background color of 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 is organized.

The JApplet class can use all 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 methods defined in the JApplet class of objects also are defined in one of its superclasses. As an example, the update() method is part of both the JApplet class and the Component class. 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 is 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 produces an undesired result.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.103.117