ABC
Category: Polymorphism
Author: Stuart Reges
Book Chapter: 9.3
Problem: ABC
Assume 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");
}
}
Consider the following code fragment:
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();
}
What output is produced by this code?