Exercise : inheritance mystery 2 practice-it

Assume the following classes have been defined:

public class Denny extends John {
    public void method1() {
        System.out.print("denny 1 ");
    }
    public String toString() {
        return "denny " + super.toString();
    }
}
public class Cass {
    public void method1() {
        System.out.print("cass 1 ");
    }
    public void method2() {
        System.out.print("cass 2 ");
    }
    public String toString() {
        return "cass";
    }
}
public class Michelle extends John {
    public void method1() {
        System.out.print("michelle 1 ");
    }
}
public class John extends Cass {
    public void method2() {
        method1();
        System.out.print("john 2 ");
    }
    public String toString() {
        return "john";
    }
}

continued on the next slide...

Exercise - inheritance mystery 2

cass 1
cass 2
cass
denny 1
denny 1 john 2
denny john
cass 1
cass 1 john 2
john
michelle 1
michelle 1 john 2
john

Consider the code below that uses these classes.
Write each line of its output in the boxes at right.

Cass[] elements = {new Cass(),
                   new Denny(),
                   new John(),
                   new Michelle()};
for (int i = 0; i < elements.length; i++) {
    elements[i].method1();
    System.out.println();
    elements[i].method2();
    System.out.println();
    System.out.println(elements[i]);
    System.out.println();
}