ABCD

Category: Polymorphism
Author: Stuart Reges
Book Chapter: 9.3
Problem: ABCD
  Assume the following classes have been defined:

        public class D extends A {
            public String toString() {
                return "d";
            }
        
            public void method1() {
                System.out.println("d 1");
            }
        }
        
        public class B extends D {
            public void method2() {
                System.out.println("b 2");
            }
        }
        
        public class A extends C {
            public void method2() {
                System.out.println("a 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");
            }
        }

   Consider the following code fragment:

        C[] elements = {new B(), new C(), new A(), 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?