single level inheritance
// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science )
class Sum {
int x, y;
Sum(int x, int y) {
this.x = x;
this.y = y;
}
void display() {
System.out.println("The value of x is " + x);
System.out.println("The value of y is " + y);
}
}
class DerivedB extends Sum {
DerivedB(int x, int y) {
super(x,y);
}
void sumOfXY() {
int sum = x + y;
System.out.println("The sum of "+x+" and "+y+" is: " + sum);
}
}
class SingleInheritance {
public static void main(String[ ] args) {
DerivedB obj = new DerivedB(6,7);
obj.display();
obj.sumOfXY();
}
}
OUTPUT
cmd-> javac SingleInheritance.java
cmd-> java SingleInheritance
The value of x is 6
The value of y is 7
The sum of 6 and 7 is: 13