Building an Inheritance Hierarchy

Inheritance, which enables a variety of related classes to be developed without redundant work, makes it possible for code to be passed down from one class to another class to another class. This grouping of classes is called a class hierarchy, and all the standard classes you can use in your Java programs are part of a hierarchy.

Understanding a hierarchy is easier if you understand subclasses and superclasses. A class that inherits from another class is called a subclass. The class that is inherited from is called a superclass.

In the preceding WarGames example, the Modem class is the superclass of the ErrorCorrectionModem class. ErrorCorrectionModem is the subclass of Modem.

A class can have more than one class that inherits from it in the hierarchy—another subclass of Modem could be ISDNModem because ISDN modems have behavior and attributes that make them different from error-correcting modems. If there was a subclass of ErrorCorrectionModem such as InternalErrorCorrectionModem, it would inherit from all classes above it in the hierarchy—both ErrorCorrectionModem and Modem. These inheritance relationships are shown in Figure 10.2.

Figure 10.2. An example of a class hierarchy.

Image

The classes that make up the standard Java language make full use of inheritance, so understanding it is essential. You learn more about inheritance during Hour 12, “Making the Most of Existing Objects.”

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

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