Create three different classes with name A, B & C respectively. All classes
have two integer variables as their private data members. Class B is child class of class A and class C is a child class of class B. Create an object of
class C and find the average of values of all its data members.
Answers
Answered by
3
Answer:
public class Test {
public static void main(String[] args) {
C obj = new C();
int avg = obj.avgC();
System.out.println("Average: "+avg);
}
}
class A {
private int a = 10, b = 20;
int avgA() {
return (a + b);
}
}
class B extends A {
private int c = 30, d = 45;
int avgB() {
return this.avgA() + (c + d);
}
}
class C extends B {
private int e = 50, f = 60;
int avgC() {
return (this.avgB() + (e + f))/6;
}
}
Similar questions