JavaScript
  Home arrow JavaScript arrow Page 5 - Multi Level Class Inheritance in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Multi Level Class Inheritance in Java
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2006-07-31

    Table of Contents:
  • Multi Level Class Inheritance in Java
  • An example of multi-level inheritance: code
  • An example of multi-level inheritance: explanation
  • Calling super class methods from a sub class: code and explanation
  • A more practical example of inheritance: code and explanation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · Hello guys. This is next contribution on OOPS with Java. enjoy
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT