Your question is Inheritance and Casting Output. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
IGT builds the software running on casino gaming machines, and this simplified device hierarchy shows up in a lot of onboarding code reviews there because new hires consistently misjudge how field hiding, static methods, and downcasting interact.
class GameDevice {
String name = "device";
String describe() {
return "GameDevice: " + name;
}
static String kind() {
return "generic";
}
}
class SlotMachine extends GameDevice {
String name = "slot";
@Override
String describe() {
return "SlotMachine: " + name;
}
static String kind() {
return "slot-machine";
}
}
public class Casino {
public static void main(String[] args) {
GameDevice device = new SlotMachine();
System.out.println(device.name);
System.out.println(device.describe());
System.out.println(device.kind());
SlotMachine slot = (SlotMachine) device;
System.out.println(slot.name);
GameDevice generic = new GameDevice();
System.out.println(((SlotMachine) generic).name);
}
}
Walk through exactly what this program prints, line by line, and explain what happens on the very last line specifically.