Tino APCS

Using Inheritance

The following program 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 number and grade point average of the student.

public class Person{
  private String myName;   // name of the person
  private int myAge;       // person's age
  private String myGender; // "M" for male, "F" for female, "NB" for non-binary

  // constructor
  public Person(String name, int age, String gender){
    myName = name;
    myAge = age;
    myGender = gender;
  }

  public String getName(){
    return myName;
  }

  public int getAge(){
    return myAge;
  }

  public String getGender(){
    return myGender;
  }

  public void setName(String name){
    myName = name;
  }

  public void setAge(int age){
    myAge = age;
  }

  public void setGender(String gender){
    myGender = gender;
  }

  public String toString(){
    return myName + , age:  + myAge + , gender: 
      + myGender;
  }
}

//-----------------End of Person Class-----------------//

public class Student extends Person{
  private String myIdNum; // Student Id Number
  private double myGPA; // grade point average

  // constructor
  public Student(String name, int age, String gender,
                                String idNum, double gpa){
    // use the super class' constructor
    super(name, age, gender);

    // initialize what's new to Student
    myIdNum = idNum;
    myGPA = gpa;
  }

  public String getIdNum(){
    return myIdNum;
  }

  public double getGPA(){
    return myGPA;
  }

  public void setIdNum(String idNum){
    myIdNum = idNum;
  }

  public void setGPA(double gpa){
    myGPA = gpa;
  }
}

//-----------------End of Student Class-----------------//

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);
    // The previous two lines could have been written as:
    // System.out.println(bob.toString());
    // System.out.println(lynne.toString());
  }
}


The Student class is a derived class (subclass) of Person. An object of type Student contains myIdNum and myGPA, which are defined in Student. It also has indirect access to the private variables myName, myAge, and myGender from Person through the methods getName(), getAge(), getGender(), setName(), setAge(), and setGender() that it inherits from Person.

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.

So far, we have only seen the public (class members that can be accessed outside the class) and private (class members that are inaccessible from outside 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. The A.P. Java subset allows the use of protected with methods but discourages its use for instance variables. It is preferred that all instance variables are private. Indirect access from subclasses should be done with public "getter" and "setter" methods. While protected members are available to provide a foundation for the subclasses to build on, they are still invisible to the public at large.

Dark Mode

Outline