logo Practice-It logo

inheritanceMystery

Author: Marty Stepp (on 2010/12/28)

Assume that the following classes have been defined:

public class Sawyer extends Jack {
    public void follow() {
        System.out.print("sawyer-F   ");
        super.follow();
    }
}

public class Kate {
    public void lead() {
        System.out.print("kate-L   ");
    }

    public void follow() {
        System.out.print("kate-F   ");
    }

    public String toString() {
        return "kate";
    }
}
public class Jack extends Locke {
    public void lead() {
        super.lead();
        System.out.print("jack-L   ");
    }
    public String toString() {
        return "jack";
    }
}

public class Locke extends Kate {
    public void follow() {
        lead();
        System.out.print("locke-F   ");
    }
}

Given the classes above, what output is produced by the following code?

Kate[] characters = {new Locke(), new Jack(), new Sawyer(), new Kate()};
for (int i = 0; i < characters.length; i++) {
    System.out.println(characters[i]);
    characters[i].lead();
    System.out.println();
    characters[i].follow();
    System.out.println();
    System.out.println();
}
elements[0]: Locke
elements[1]: Jack
elements[2]: Sawyer
elements[3]: Kate

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.