Level Inheritance Using Java


Java implements level inheritance very similarly to how level inheritance is implemented in C++, as shown in this next example. The same three classes defined in the C++ program are also defined in this Java application. Each class has the same attributes and member methods that perform the same functionality as their counterparts in the C++ program.

The Person class is inherited by the Student class using the keyword extends . The Student class is inherited by the GradStudent class also using the keyword extends . The Display() method of the Student class and of the GradStudent class each call the Display() method of its super class in order to display values of the super class s attributes. This is similar to how attributes of a super class are displayed in simple inheritance using Java.

The main() method in this example contains practically the same statements found in the simple inheritance example, except the Write() method is passed information about a person as well as information about a student and a graduate student. The Display() method within the main() method displays values of attributes of all the classes on the screen.

Here is what is displayed on the screen when you run the following program:

ID: 100
First: Mike
Last: Lee
Graduation: 2008
ID: 101
First: Marta
Last: Enriquez
Graduation: 2008
Major: Computer Science
Undergrad Graduation year: 2002
Undergrad School: Columbia

 class Person { 
protected int nID;
protected String sFirst, sLast;
public Person() {
nID = 0;
sFirst = "";
sLast = "";
}
public void Display() {
System.out.println("ID: " + nID);
System.out.println("First: " + sFirst);
System.out.println("Last: " + sLast);
}
public void Write( int ID, String First, String Last ) {
nID = ID;
sFirst = First;
sLast = Last;
}
}
class Student extends Person{
protected int nGraduation;
public Student() {
nGraduation = 0;
}
public void Display() {
super.Display();
System.out.println("Graduation: " + nGraduation);
}
public void Write( int ID, String First, String Last, int Graduation ) {
super.Write(ID, First, Last);
nGraduation = Graduation;
}
}
class GradStudent extends Student {
String sMajor, sUndergradSchool;
int nUndergradGraduation;
public GradStudent() {
sMajor = "";
sUndergradSchool="";
nUndergradGraduation=0;
}
public void Display() {
super.Display();
System.out.println("Major: " + sMajor );
System.out.println("Undergrad Graduation year: " +
nUndergradGraduation);
System.out.println("Undergrad School: " + sUndergradSchool );
}
public void Write( int ID, String First, String Last, int Graduation,
String Major, String UndergradSchool, int UndergradGraduation ) {
super.Write( ID, First, Last, Graduation);
sUndergradSchool = UndergradSchool;
sMajor = Major;
nUndergradGraduation = UndergradGraduation;
}
}
public class StudentApp {
public static void main(String[] args) {
Student s = new Student();
GradStudent g = new GradStudent();
s.Write( 100, "Mike", "Lee", 2008);
g.Write(101, "Marta", "Enriquez", 2008, "Computer Science", "Columbia", 2002);
s.Display();
g.Display();
}
public static void Display( Student s ) {
s.Display();
}
public static void Display( GradStudent g ) {
g.Display();
}
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net