2021年5月5日星期三

Accessing private superclass field in subclass

I know that private fields in a superclass are not inherited by the subclass but my assignment requires me to access a private field from a subclass. There is a getter method for it by problem comes from referencing the private variable directly in one of the subclasses methods:

protected void userPicksUp(Player player) {        System.out.println("What would you like to pick up?");        Scanner keyboard = new Scanner(System.in);        String itemToPickUp = keyboard.nextLine();        if (Utilities.isItemInContainer(itemToPickUp, getRoomObjects())) {           player.addToInventory(itemToPickUp);           roomObjects = Utilities.removeFromList(itemToPickUp, getRoomObjects());        } else {           System.out.println("That item is not in the room");        }     }  

I can't figure out how to access roomObjects from the superclass for this subclass method. I cannot make any new fields for this assignment. I have also used super() for the subclass' constructor and roomObjects came with that so I don't really understand why I can't access it if it is part of my subclass' constructor?

https://stackoverflow.com/questions/67411229/accessing-private-superclass-field-in-subclass May 06, 2021 at 10:57AM

没有评论:

发表评论