Multi Level Class Inheritance in Java - A more practical example of inheritance: code and explanation
(Page 5 of 5 )
In previous sections, I simply provided a chain type of multi-level inheritance. Now, I shall give you an example of tree type inheritance.
Add three more classes, “Person”, “Lecturer” and “Student,” to your solution. Now, modify the class “Person” so that it looks something like the following:
public class Person {
String name;
/** Creates a new instance of Person */
public Person() {
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;
}
}
Now modify the class “Lecturer” so that it looks something like the following:
public class Lecturer extends Person {
String qualification;
/** Creates a new instance of Lecturer */
public Lecturer() {
}
public Lecturer(String n, String q) {
name = n;
qualification = q;
}
public String getQualification() {
return qualification;
}
}
Now modify the class “Student” so that it looks something like the following:
public class Student extends Person {
String course;
/** Creates a new instance of Student */
public Student() {
}
public Student(String n, String c) {
name = n;
course = c;
}
public String getCourse() {
return course;
}
}
Finally modify your “test.java” so that it looks something like the following:
private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Student s1 = new Student("Jag", "B.Sc.");
Lecturer l1 = new Lecturer("Chat", "M.Sc.");
this.lblMsg.setText("Student Name = " + s1.getName());
this.lblMsg2.setText("Lecturer Name = " + l1.getName());
}
Here is the explanation for the example provided above. I simply declared three classes, namely “Person”, “Student” and “Lecturer.” The relationship between these three classes can be explained as follows:
- Every “Student” is a “Person”
- Every “Lecturer” is a “Person”
- Any “Student” is not a “Lecturer”
- Any “Lecturer” is not a “Student”
From the above, we can decide that “Student” or “Lecturer” belong to the “Person” category. Thus “Person” will be the super (or parent) class for both “Student” and “Lecturer.” Conversely, “Student” and “Lecturer” both get inherited from “Person.” All the members available in “Person” will be shared by both “Student” and “Lecturer” (but individually for each instance). You can also observe that “Student” has its own individual members, which are quite different from the individual members of “Lecturer.”
The entire code for this article is freely available in the form of a zip file. That downloadable solution was developed using NetBeans 4.1 IDE together with Microsoft Windows 2003 Standard Edition. I didn’t really test it in any other version or platform. Please follow the respective platform documentation to get it working. This series is dedicated to the beginners who wanted to work with NetBeans IDE.
Any doubts, bugs, errors, suggestions, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |