Skip to content
Advertisement

How do you have a class change the value of an attribute of another class?

Let’s say I have a class named Door with an attribute that states whether it’s open or closed. Then, there is another class named Person, that is assigned to that door.

How would you tell the person to open/close the door by changing the value of the attribute in the Door class with a method in the Person class?

Advertisement

Answer

You mean something like this?

class Door {
    public boolean closed = true;
}

class Person {
    Door door = new Door();

    public void changeDoorState(boolean closed) {
        door.closed = closed;
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement