Nathan Sprague
public class ToyRobot extends LoudToy {
private int chargeLevel;
public ToyRobot() {
chargeLevel = 5;
}
@Override
public void makeNoise() {
System.out.println("Beep Beep!");
}
public void recharge() {
chargeLevel = 10;
System.out.println("Charged up!");
}
}
Quiz…
What will be printed?
What will be printed?
Generic noise.
Generic noise.
Baaa.
Beep Beep!
Generic noise.
Baaa.
Generic noise.
Beep Beep!
Which code sample will result in the following terminal output?
Beep Beep!
Charged up!
LoudToy toy;
ToyRobot bot;
toy = new ToyRobot();
toy.makeNoise();
bot = (ToyRobot)toy;
bot.recharge();
None of the above.
More than one of the above.
Will it compile?
Will it execute without an exception?
If so, what will be printed?
Will it compile?
YES! The Java compiler will take our word for it that the assignment on line 4 makes sense.
Will it execute without an exception?
NO! The assignment on line 4 will result in an exception. A ToySheep IS NOT a ToyRobot, so it can never be stored in a ToyRobot variable.
If so, what will be printed?
One option…
Protected members are visible in all subclasses (and all other classes in the same package).
/**
* Utility methods to help us play with toys.
*/
public class PlayUtils {
public static void playWithLoudToy(LoudToy toy) {
System.out.println("This is a fun loud toy!");
toy.makeNoise();
}
public static void playWithLoudToy(ToyRobot robot) {
System.out.println("This is a fun robot!");
robot.makeNoise();
}
}
Which overloaded method will be called is determined at compile time based on the type of the variable that is passed as an argument. If there is no exact match, the closest ancestor will be selected:
ToyRobot bot = new ToyRobot();
LoudToy toy = bot;
ToySheep sheep = new ToySheep();
PlayUtils.playWithLoudToy(bot); // Second method is called. (ToyRobot version)
PlayUtils.playWithLoudToy(toy); // First method is called. (LoudToy version)
// even though the toy variable
// actually contains a ToyRobot.
PlayUtils.playWithLoudToy(sheep); // First method is called, even though
// ToySheep is not the parameter type.
// a ToySheep is-a LoudToy, and there
// is no version that expects a ToySheep
// explicitly.
This is a fun robot!
Beep Beep!
This is a fun loud toy!
Beep Beep!
This is a fun loud toy!
Baaa.