📖 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
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
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
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
Hint
Create the object using val device = SmartDevice() and call the functions in order.
Practice
(1/5)
1. What does it mean when a Kotlin class implements multiple interfaces?
easy
A. The class cannot have its own functions if it implements interfaces.
B. The class can only use one interface's functions at a time.
C. The class agrees to provide all functions defined in all those interfaces.
D. The class automatically inherits properties from all interfaces without code.
Solution
Step 1: Understand interface implementation
Implementing interfaces means the class must provide the functions declared in those interfaces.
Step 2: Check multiple interfaces
When multiple interfaces are implemented, the class must provide all functions from each interface.
Final Answer:
The class agrees to provide all functions defined in all those interfaces. -> Option C
Quick Check:
Multiple interface implementation = implement all interface functions [OK]
Hint: Remember: Implementing means providing all interface functions [OK]
Common Mistakes:
Thinking interfaces provide code automatically
Believing only one interface's functions are needed
Assuming class can't have its own functions
2. Which of the following is the correct syntax to declare a Kotlin class MyClass implementing interfaces InterfaceA and InterfaceB?
easy
A. class MyClass : InterfaceA, InterfaceB {}
B. class MyClass implements InterfaceA, InterfaceB {}
C. class MyClass extends InterfaceA, InterfaceB {}
D. class MyClass : InterfaceA & InterfaceB {}
Solution
Step 1: Recall Kotlin interface syntax
Kotlin uses a colon (:) to implement interfaces, separated by commas.
Step 2: Check each option
class MyClass : InterfaceA, InterfaceB {} uses colon and commas correctly. Options B and C use Java-like keywords. class MyClass : InterfaceA & InterfaceB {} uses & which is invalid in Kotlin.
Final Answer:
class MyClass : InterfaceA, InterfaceB {} -> Option A
Quick Check:
Use ':' and commas for interfaces [OK]
Hint: Use ':' and commas to list interfaces in Kotlin [OK]
Common Mistakes:
Using 'implements' keyword like Java
Using 'extends' for interfaces
Using '&' to combine interfaces
3. What will be the output of this Kotlin code?
interface A { fun greet() = println("Hello from A") }
interface B { fun greet() = println("Hello from B") }
class C : A, B {
override fun greet() {
super<A>.greet()
super<B>.greet()
}
}
fun main() {
val c = C()
c.greet()
}
medium
A. Hello from A\nHello from B
B. Hello from B\nHello from A
C. Hello from A
D. Compilation error due to greet() conflict
Solution
Step 1: Understand interface default methods
Both interfaces A and B have greet() with default implementations.
Step 2: Check class C's override
C overrides greet() and calls both super<A>.greet() and super<B>.greet() in order.
Step 3: Predict output
First prints "Hello from A", then "Hello from B" on separate lines.
Final Answer:
Hello from A
Hello from B -> Option A
Quick Check:
Calling both super interface methods prints both greetings [OK]
Hint: Use super<InterfaceName> to call specific interface method [OK]
Common Mistakes:
Assuming compilation error due to method conflict
Only calling one interface's greet()
Mixing order of greetings
4. Identify the error in this Kotlin code where class D implements interfaces X and Y:
interface X { fun foo() }
interface Y { fun bar() }
class D : X, Y {
override fun foo() { println("foo") }
}
medium
A. Cannot implement two interfaces at once
B. Missing override of bar() from interface Y
C. foo() should not be overridden
D. Class D should be abstract
Solution
Step 1: Check implemented interfaces
D implements X and Y, so must override all their functions.
Step 2: Verify overridden functions
D overrides foo() from X but does not override bar() from Y.
Step 3: Identify error
Missing override of bar() causes compilation error.
Final Answer:
Missing override of bar() from interface Y -> Option B
Quick Check:
All interface functions must be overridden [OK]
Hint: Override all interface functions or make class abstract [OK]
Common Mistakes:
Forgetting to override all interface methods
Thinking multiple interfaces are not allowed
Assuming override is optional
5. You have two interfaces:
interface Printer { fun print() }
interface Scanner { fun scan() }
You want a class MultiFunctionDevice that implements both and logs each action. Which is the best way to implement this?
hard
A. class MultiFunctionDevice : Scanner {
override fun scan() { println("Scanning...") }
}
B. class MultiFunctionDevice : Printer {
override fun print() { println("Printing...") }
}
C. class MultiFunctionDevice {
fun print() { println("Printing...") }
fun scan() { println("Scanning...") }
}
D. class MultiFunctionDevice : Printer, Scanner {
override fun print() { println("Printing...") }
override fun scan() { println("Scanning...") }
}
Solution
Step 1: Understand requirement
MultiFunctionDevice must implement both Printer and Scanner interfaces.
Step 2: Check options
class MultiFunctionDevice : Printer, Scanner {
override fun print() { println("Printing...") }
override fun scan() { println("Scanning...") }
} implements both interfaces and overrides both functions with logging. Options A and B implement only one interface each. class MultiFunctionDevice {
fun print() { println("Printing...") }
fun scan() { println("Scanning...") }
} does not implement interfaces.
Final Answer:
class MultiFunctionDevice : Printer, Scanner {
override fun print() { println("Printing...") }
override fun scan() { println("Scanning...") }
} -> Option D
Quick Check:
Implement all interfaces and override all methods [OK]
Hint: Implement all interfaces and override all their methods [OK]
Common Mistakes:
Implementing only one interface when multiple are needed
Not overriding interface methods
Not declaring interfaces in class header
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.