«^»
7.3. A class called Student

Here is a class declaration for the class Student:

0558: import java.util.StringTokenizer;                            // Student.java
0559: public class Student extends Person {
0560:    public Student(String pName, float pHeight, Date pDateOfBirth,
0561:                   String pCourseName, int pStudentNumber) {
0562:       super(pName, pHeight, pDateOfBirth);
0563:       iCourseName = pCourseName; iStudentNumber = pStudentNumber;
0564:    }
0565:    public int getStudentNumber() { return iStudentNumber; }
0566:    public boolean equals(Object pObject) { 
0567:       return oName.equals(((Student) pObject).oName);
0568:    }
0569:    public String toString() { 
0570:       return oName + "=" + oDateOfBirth 
0571:                    + "=" + iCourseName + "=" + iStudentNumber;
0572:    }
0573:    private String iCourseName;
0574:    private int iStudentNumber;
0575: }

Suppose you declare an object to be of the subclass:

0576: Student tStudent = new Student( ... );
As well as having the members of the subclass, the object has all the members of the superclass. So, the object tStudent has the members: from the class Student and the following members: from the class Person.

So an object of the class Student has five fields that are called oName, iHeight, oDateOfBirth, iCourseName and iStudentNumber. The constructor for the class Student has arguments that are used to initialize not only the fields of the class Student but also the fields from the class Person. In the body of the constructor, a special method called super is used to initialize the fields of the superclass (Person).

Because each of these five fields is declared to be private or protected, they are inaccessible to a client of the class Student. However, a client can use any public members of the class or any public members of the superclass. Examples are:

0577: System.out.println(tStudent.getName());
0578: System.out.println(tStudent.getStudentNumber());