0
0
Kotlinprogramming~10 mins

Multiple interface implementation in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple interface implementation
Define Interface A
Define Interface B
Create Class implementing A & B
Override methods from A & B
Create instance of Class
Call methods from both interfaces
Observe combined behavior
This flow shows how a class can implement multiple interfaces by overriding their methods and then using those methods through an instance.
Execution Sample
Kotlin
interface A {
    fun foo() = println("A's foo")
}
interface B {
    fun bar() = println("B's bar")
}
class C : A, B

fun main() {
    val c = C()
    c.foo()
    c.bar()
}
This code defines two interfaces A and B, a class C implementing both, and calls their methods.
Execution Table
StepActionEvaluationResult
1Define interface A with foo()foo() prints "A's foo"Interface A ready
2Define interface B with bar()bar() prints "B's bar"Interface B ready
3Define class C implementing A and BC inherits foo() and bar()Class C ready
4Create instance c of class Cc is instance of CInstance c created
5Call c.foo()Calls foo() from APrints: A's foo
6Call c.bar()Calls bar() from BPrints: B's bar
7Program endsNo more callsExecution stops
💡 All interface methods called, program ends normally
Variable Tracker
VariableStartAfter Step 4After Step 7
cundefinedInstance of CInstance of C (no change)
Key Moments - 2 Insights
Why can class C call both foo() and bar() methods?
Because class C implements both interfaces A and B, it inherits their methods, so calling c.foo() and c.bar() works as shown in steps 5 and 6 of the execution_table.
What happens if interfaces A and B have methods with the same name?
Then class C must override that method explicitly to resolve the conflict, which is not shown here but important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when c.foo() is called at step 5?
A"foo from C"
B"B's bar"
C"A's foo"
DNothing is printed
💡 Hint
Check the Result column at step 5 in execution_table
At which step is the instance c of class C created?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the Action column for instance creation in execution_table
If class C did not implement interface B, what would happen when calling c.bar() at step 6?
AIt would cause a compile error
BIt would print "B's bar" anyway
CIt would call foo() instead
DIt would print nothing
💡 Hint
Recall that c.bar() comes from interface B, see execution_table step 6
Concept Snapshot
Multiple interface implementation in Kotlin:
- Use 'class C : A, B' to implement interfaces A and B
- Override interface methods if needed
- Class inherits all interface methods
- Create instance and call methods from all interfaces
- Resolves method conflicts by overriding explicitly
Full Transcript
This example shows how Kotlin allows a class to implement multiple interfaces. Interfaces A and B each have one method. Class C implements both interfaces, inheriting their methods. When we create an instance of C and call foo() and bar(), the program prints messages from both interfaces. This demonstrates how multiple interface implementation combines behaviors from different sources into one class.