educative.io

Code of Elevator System

public abstract class Button {
private boolean status;

public pressDown();
public abstract boolean isPressed();

}

in button class what does status means and what is the role of pressDown and isPressed button. If isPreseed() just return bool then why have be made it abstract function.


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Code of Elevator System

Hi @Yuk,
The code of the elevator button is explained here:

  • The status variable is used to store the current state of the button (pressed or not pressed).
  • The pressDown() method is responsible for simulating the action of pressing the button, which typically involves changing the state of the button to “pressed.”
  • The isPressed() method, when implemented in subclasses like DoorButton, HallButton, and ElevatorButton, checks and returns whether the button is currently pressed (true) or not pressed (false). The reason for making isPressed() abstract is to enforce that each specific type of button must define how its pressed state is determined.

I hope it helps. Happy Learning!