Home arrow JavaScript arrow Page 2 - 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 - An example of multi-level inheritance: code
(Page 2 of 5 )

I already introduced single inheritance in my previous article. In this section, I shall introduce you to the concept of multi-level inheritance.

Open your NetBeans project (as given in my previous article), and modify your code in “first.java” so that it looks something like the following:

  public class First {

      int x=0;
      int y=0;

      /** Creates a new instance of First */
      public First() {
          x = 10;
          y = 20;
      }

      public void setValues(int m, int n) {
          x = m;
          y = n;
      }

      public int getSum() {
          int t;
          t = x + y;
          return t;
      }
  }

Modify your code in “second.java” so that it looks something like the following:

  public class Second extends First {

    /** Creates a new instance of Second */
    public Second() {
        x = 90;
        y = 80;
    }

    public Second(int m, int n) {
        x=m;
        y=n;
    }

    public int getProduct() {
        int p;
        p = x * y;
        return p;
    }
  }

Now add a new class named “Third” using your NetBeans IDE. Modify your code in “Third.java” so that it looks something like the following:

  public class Third extends Second {

    /** Creates a new instance of Third */
    public Third() {
    }

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

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.getDifference();
        this.lblMsg2.setText("Difference = " + String.valueOf(r));

    }
       

The next section will explain all of the above code.


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