0
0
Kotlinprogramming~30 mins

Multiple interface implementation in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Multiple Interface Implementation in Kotlin
📖 Scenario: Imagine you are building a simple app that manages different types of devices. Each device can perform certain actions like turning on/off and connecting to a network. Some devices can do both, so you need to use multiple interfaces to represent these capabilities.
🎯 Goal: You will create two interfaces Switchable and Connectable, then create a class SmartDevice that implements both interfaces. Finally, you will create an object of SmartDevice and call its methods to see the output.
📋 What You'll Learn
Create an interface called Switchable with two functions: turnOn() and turnOff().
Create an interface called Connectable with one function: connect().
Create a class called SmartDevice that implements both Switchable and Connectable interfaces.
Implement all interface functions in SmartDevice with simple print statements describing the action.
Create an instance of SmartDevice and call all three functions to display their messages.
💡 Why This Matters
🌍 Real World
Many devices and software components have multiple capabilities. Using multiple interfaces helps organize code and reuse functionality.
💼 Career
Understanding multiple interface implementation is important for designing flexible and maintainable software in Kotlin and many other languages.
Progress0 / 4 steps
1
Create the interfaces
Create an interface called Switchable with two functions: turnOn() and turnOff(). Also create an interface called Connectable with one function: connect().
Kotlin
Need a hint?

Use the interface keyword to create interfaces. Define functions without bodies inside them.

2
Create the SmartDevice class
Create a class called SmartDevice that implements both Switchable and Connectable interfaces.
Kotlin
Need a hint?

Use a colon : to implement multiple interfaces separated by commas. Use override keyword to provide function bodies.

3
Implement interface functions
Inside the SmartDevice class, implement the functions turnOn(), turnOff(), and connect() to print messages: "SmartDevice is turned on", "SmartDevice is turned off", and "SmartDevice is connecting to network" respectively.
Kotlin
Need a hint?

Use println() inside each function to show the messages.

4
Create object and call functions
Create an instance of SmartDevice called device. Then call device.turnOn(), device.connect(), and device.turnOff() to display the messages.
Kotlin
Need a hint?

Create the object using val device = SmartDevice() and call the functions in order.