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:
Create an open class called
DeviceControl
with primary constructor propertiesid
andisEnabled
.Implement the
enable
anddisable
methods in theDeviceControl
class, altering theisEnabled
property.Create two subclasses of
DeviceControl
:ButtonControl
andSwitchControl
.The
ButtonControl
subclass should possess a constructor with a parameterid
. It sets theisEnabled
property of the superclass tofalse
.The
SwitchControl
subclass should have a constructor with the parameterid
. It sets theisEnabled
property of the superclass tofalse
.The
SwitchControl
subclass should introduce an additional propertyisOn
, initially set tofalse
.The
SwitchControl
subclass should include atoggle
method that will change theisOn
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 1400+ tech skills courses.