Exercise: Simulating a Device Controls Hierarchy

Refine your Kotlin proficiency by developing a simulation of device controls with open and subclassed controls.

We'll cover the following

Problem statement

Implement the following classes and methods to represent a hierarchy of device controls:

  1. Create an open class called DeviceControl with primary constructor properties id and isEnabled.

  2. Implement the enable and disable methods in the DeviceControl class, altering the isEnabled property.

  3. Create two subclasses of DeviceControl: ButtonControl and SwitchControl.

  4. The ButtonControl subclass should possess a constructor with a parameter id. It sets the isEnabled property of the superclass to false.

  5. The SwitchControl subclass should have a constructor with the parameter id. It sets the isEnabled property of the superclass to false.

  6. The SwitchControl subclass should introduce an additional property isOn, initially set to false.

  7. The SwitchControl subclass should include a toggle method that will change the isOn property to the opposite state.

Instructions

In the given code, you have the initial setup of classes for simulating a hierarchy of device controls. Your task is to implement the device controls simulation classes and methods as described above.

The actual output when running the provided code should be as follows:

Get hands-on with 1200+ tech skills courses.