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.
Next: An example of multi-level inheritance: explanation >>
More JavaScript Articles
More By Jagadish Chaterjee