Home arrow JavaScript arrow Page 5 - Multi Level Class Inheritance in Java
JAVASCRIPT

Multi Level Class Inheritance in Java


In this article, I shall provide an introduction to multi-level inheritance when programming with OOPS in Java using NetBeans IDE. I will also cover calling methods of the super class, and discuss a simple, practical example.

Author Info:
By: Jagadish Chaterjee
Rating: 3 stars3 stars3 stars3 stars3 stars / 19
July 31, 2006
TABLE OF CONTENTS:
  1. · Multi Level Class Inheritance in Java
  2. · An example of multi-level inheritance: code
  3. · An example of multi-level inheritance: explanation
  4. · Calling super class methods from a sub class: code and explanation
  5. · A more practical example of inheritance: code and explanation

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials