We originally weren't going to cover this in the lectures, but since it's covered in the text book, and since I was asked about it in lecture last week, I thought I'd explain how it really works...
This example shows you how you can inherit things from multiple levels in a hierarchy (for example, the StudentAthlete class can inherit from Student and Person). It also shows you how we can make instance variables that are declared in Person visible in its subclasses, using the protected (or public) modifier.
Here is a look at the class hierarchy that we'll use for this example. We will look at the Person, Student, and StudentAthlete classes.
Person / | \ / | \ Professor Student Employee / | \ / | \ / | \ StudentMusician StudentAthlete StudentClubMem
The Person class stores general information about a person. It has instance variables to store the name and birthdate of a person. These things are common to all people, so it makes sense to store it at the top of the hierarchy, in the Person class.
The Student class stores a student's ID number. A student object inherits everything from its parent class, Person, plus it adds on new information about the student's ID number.
A StudentAthlete is a Student, and a Student is a Person. The StudentAthlete class has an instance variable called "sport" that stores the name of the sport in which this athlete is involved.
Inherited variables cannot be used in a child class unless they have been declared as either public or protected. Declaring them as public is not a good idea because this gives all classes (whether inherited or not) access to these members. Instead Java provides a special modifier called "protected." When a variable or method has been declared as "protected" it means that only subclasses of the class can access the variable.
// Person: a person has a name and a birthdate. class Person { protected String name; protected String bdate; public Person (String name, String bdate) { this.name = name; this.bdate = bdate; } public String toString () { return "Name: " + name + "\n" + "Birth Date: " + bdate; } } // Student: a student is a person, and has a student number. class Student extends Person { protected String stunum; public Student (String name, String bdate, String num) { super (name, bdate); this.stunum = num; } public String toString () { String result = super.toString(); return result + "\n" + "ID: " + stunum; } } // StudentAthlete: a student athlete is a student, and has a sport. class StudentAthlete extends Student { protected String sport; public StudentAthlete (String name, String bdate, String num, String sport) { super (name, bdate, num); this.sport = sport; } public String toString () { String result = super.toString(); return result + "\n" + "Sport: " + sport; } // Since the name, dbate, and stunum variables have been // inherited, and since they have been declared as protected // (public would have worked too), we can use them here. public void test () { System.out.println ("All info stored: " + name + " " + bdate + " " + stunum + " " + sport); } } // School: this is the test class used to create a StudentAthlete // object, and to call a test method on this object. class School { public static void main (String[] args) { StudentAthlete sa = new StudentAthlete ("Jane", "1980", "999333222", "hockey"); sa.test(); System.out.println (); System.out.println (sa); } }
All info stored: Jane 1980 999333222 hockey Name: Jane Birth Date: 1980 ID: 999333222 Sport: hockey
[ CSC108 Home | Outline | Announcements | Newsgroup | Assignments | Lectures | Marks | Links ]
© Copyright 2000. All Rights Reserved.