Skip to main content
Lesson 14 - Inheritance
Lesson MenuPreviousNext
  
Using Inheritance page 5 of 10

  1. Here is a program that uses a class Person to represent people you might find at a school. The Person class has basic information in it, such as name, age and gender. An additional class, Student, is created that is similar to Person, but has the id and grade point average of the student.

    class Person
    {
      protected String myName ;   // name of the person
      protected int myAge;        // person's age
      protected String myGender;  // "M" for male, "F" for female
    
      // constructor
      public Person(String name, int age, String gender)
      {
        myName = name; myAge = age ; myGender = gender; 
      }
    
      public String toString()
      {
        return myName + ", age: " + myAge + ", gender: " +myGender;
      }
    }
    
    class Student extends Person
    {
      protected String myIdNum;    // Student Id Number
      protected double myGPA;      // grade point average
    
      // constructor
      public Student(String name, int age, String gender,
                     String idNum, double gpa)
      {
        // use the super class's constructor
        super(name, age, gender);
    
        // initialize what's new to Student
        myIdNum = idNum;
        myGPA = gpa;
      }
    }
    
    public class HighSchool
    {
      public static void main (String args[])
      {
        Person bob = new Person("Coach Bob", 27, "M");
        Student lynne = new Student("Lynne Brooke", 16, "F",
                                     "HS95129", 3.5);
        System.out.println(bob);
        System.out.println(lynne);
      }
    }
  2. The Student class is a derived class (subclass) of Person. An object of type Student contains the following members:

     Member 
       
     myNameinherited from Person
     myAgeinherited from Person
     myGenderinherited from Person
     toString()inherited from Person
     myIdNuminherited from Student
     myGPAinherited from Student

  3. The constructor for the Student class initializes the instance data of Student objects and uses the Person class's constructor to initialize the data of the Person superclass. The constructor for the Student class looks like this:

    // constructor
    public Student(String name, int age, String gender,
                   String idNum, double gpa)
    {
      // use the super class's constructor
      super(name, age, gender);
    
      // initialize what's new to Student
      myIdNum = idNum;
      myGPA = gpa;
    }

    The statement super(name, age, gender) invokes the Person class's constructor to initialize the inherited data in the superclass. The next two statements initialize the members that only Student has. Note that when super is used in a constructor, it must be the first statement.

  4. It is not necessary to use super; the following would also work as a constructor for Student:

    // constructor
    public Student(String name, int age, String gender,
                   String idNum, double gpa)
    {
      // initialize the inherited members
      myName = name;
      myAge = age ;
      myGender = gender;
    
      // initialize what's new to Student
      myIdNum = idNum;
      myGPA = gpa;
    }

    In this constructor, each variable of the newly created Student object is set to an initial value.

  5. So far, we have only seen the public (class members that are inaccessible from outside of the class) and private (class members that can be accessed outside of the class) access modifiers. There is a third access modifier that can be applied to an instance variable or method. If it is declared to be protected, then it can be used in the class in which it is defined and in any subclass of that class. This declaration is less restrictive than private and more restrictive than public. Classes that are written specifically to be used as a basis for making subclasses often have protected members. The protected members are there to provide a foundation for the subclasses to build on. But they are still invisible to the public at large.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.