ABCD
Category: Polymorphism
Author: Stuart Reges
Book Chapter: 9.3
Problem: ABCD
Assume the following classes have been defined:
public class A extends B {
public String toString() {
return "a";
}
}
public class B extends D {
public void method1() {
System.out.println("b 1");
}
public void method2() {
System.out.println("b 2");
}
}
public class C extends D {
public void method1() {
System.out.println("c 1");
}
}
public class D {
public String toString() {
return "d";
}
public void method1() {
System.out.println("d 1");
}
public void method2() {
System.out.println("d 2");
}
}
Consider the following code fragment:
D[] elements = {new B(), new A(), new D(), new C()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
elements[i].method2();
System.out.println();
}
What output is produced by this code?