Question: I'm doing a java project and I need to override a method from the superclass in the subclass. The method in the superclass is public,
I'm doing a java project and I need to override a method from the superclass in the subclass. The method in the superclass is public, but it uses a private method. I posted the relevant code of the superclass below and so far what I have of the sub class. The instructions we are given are: "Add a playRoom(Player player) method to the subclass, which will override the playRoom method in the superclass. Its only argument should be a reference variable to a single Player object. There are a few technical private/public issues that you'll need to contend with when you write the overriding playRoom method." We were also unstructed to not make any changes to the superclass. Is there anyway to write an overriding method in the subclass that invokes the private method in the superclass getNextRoom?
public class Room {
// fields
private String roomObjects;
private String roomName;
private String roomExits;
private int roomNum;
// The playRoom method can be invoked with either
// a CustomPlayer object OR a Player object. They
// both invoke the same method, getNextRoom
public int playRoom(CustomPlayer player) {
return getNextRoom(player);
}
public int playRoom(Player player) {
return getNextRoom(player);
}
private int getNextRoom(Player player) {
public class MonsterRoom extends Room { public MonsterRoom(String roomName, String roomObjects, int roomNum, String roomExits) { super(roomName, roomObjects, roomNum, roomExits); } @Override public int playRoom(Player player) { return super.getNextRoom(player); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
