Home arrow JavaScript arrow Page 4 - 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 - Calling super class methods from a sub class: code and explanation
(Page 4 of 5 )

In this section, we will look at calling super class methods from a sub class using multi-level inheritance. The same issues apply to single inheritance.

Now, open the class “Third” (available in “third.java”) and modify the code in such a way that it looks like the following:

  public class Third extends Second {

    int z;

    /** Creates a new instance of Third */
    public Third() {
        z=50;
    }

    public int getDifference() {
        int p;
        p = x - y;
        return p;
    }

    public int getProductOfAll() {
        return ((super.getProduct()) * z);
    }
}

Now go back to the frame “test.java.” Double click on it to open and finally double click on the button to open the source view.  Within the source view, modify your “buttonActionPerformed” in such a way that it looks like the following:

  private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
  // TODO add your handling code here:
        Third obj1 = new Third();
        int r;
        r = obj1.getProductOfAll();
        this.lblMsg2.setText("Product = " + String.valueOf(r));
    }
               

From all of the above code, the only important issue to discuss is the following method:

  public int getProductOfAll() {
        return ((super.getProduct()) * z);
    }

This method simply calls another method, “getProduct,” which belongs to its super class.  As “getMethod” returns an integer, it can be multiplied with “z” and again return the same.

In this case, the word “super” in the code is not essential.  But makes the code more readable.


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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials