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.
Next: A more practical example of inheritance: code and explanation >>
More JavaScript Articles
More By Jagadish Chaterjee