1. Assume that the following classes have been defined: public class A extends B
ID: 3868349 • Letter: 1
Question
1. Assume that the following classes have been defined:
public class A extends B {
public void method2() {
System.out.println("a 2");
}
}
public class B extends C {
public String toString() {
return "b";
}
public void method2() {
System.out.println("b 2");
}
}
public class C {
public String toString() {
return "c";
}
public void method1() {
System.out.println("c 1");
}
public void method2() {
System.out.println("c 2");
}
}
public class D extends B {
public void method1() {
System.out.println("d 1");
}
}
Given the classes above, what output is produced by the following code? (8 points)
C[] elements = {new A(), new B(), new C(), new D()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
elements[i].method2();
System.out.println();
}
Explanation / Answer
Output will be
b
c 1
a 2
b
c 1
b 2
c
c 1
c 2
b
d 1
b 2
The output of the program depends on the overriding of the methods method1, method2.
And with inheritance the object of class C become capable of holding information of classes A, B, C, and D
PLEASE RATE !!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.