Building an Inheritance Hierarchy

Inheritance, which enables a variety of related classes to be developed without a lot of redundant work, makes it possible for code to be passed down from one class to another class to another class. This organization of classes is called a class hierarchy, and all of the standard classes you can use in your Java programs are part of a hierarchy. Understanding a hierarchy is easier if you understand what subclasses and superclasses are. A class that inherits from another class is called a subclass, and the class that is inherited from is called a superclass. In the preceding War Games 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, since ISDN modems have behavior and attributes that make them different from error-correcting modems. If there were 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 Screenshot.

Screenshot An example of a class hierarchy.

Java ScreenShot


The programs you write as you are learning about Java won't use complicated class hierarchies. However, the classes that are part of the standard Java language make full use of inheritance. Understanding it is essential to working with the classes that are part of the Java language. You'll learn more about inheritance during Hour 12, "Making the Most of Existing Objects."

      
Comments