Skip to main content
Lesson 14 - Inheritance
ZIPPDF (letter)
Lesson MenuPreviousNext
  
Class Hierarchies page 4 of 10

  1. In a hierarchy, each class has at most one superclass, but might have several subclasses. There is one class, at the "top" of the hierarchy that has no superclass. This is sometimes called the root of the hierarchy.

    Figure 14.3 - Person Inheritance Hierarchy.

    The figure shows a hierarchy of classes. It shows that "Principal is a kind of Person," "Student is a kind of Person," and that "Teacher is a kind of Person." It also shows that "HighSchoolStudent is a kind of Student" and "CollegeStudent is a kind of Student."

  2. In our example, the class Person is the base class and the classes Principal, Student, Teacher, HighSchoolStudent, and CollegeStudent are derived classes.

  3. In Java, the syntax for deriving a child class from a parent class is:

    class subclass extends superclass
    {
      // new characteristics of the subclass go here
    }
  4. Several classes are often subclasses of the same class. A subclass may in turn become a parent class for a new subclass. This means that inheritance can extend over several "generations" of classes. This is shown in the diagram, where class HighSchoolStudent is a subclass of class Student, which is itself a subclass of the Person class. In this case, class HighSchoolStudent is considered to be a subclass of the Person class, even though it is not a direct subclass.

  5. In Java, every class that does not specifically extend another class is a subclass of the class Object. For example, in Figure 14.3, the Person class extends the Object class. The Object class has a small number of methods that make sense for all objects, such as the toString method, but these methods are not very useful and usually get redefined in classes lower in the hierarchy


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.