0
0
Kotlinprogramming~10 mins

Interface with default implementations in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interface with default implementations
Define Interface
Add Default Method
Implement Interface in Class
Use Class Instance
Call Method
Class Override?
YesUse Override
No
Use Default Implementation
This flow shows how an interface with a default method is defined, implemented by a class, and how method calls use either the class override or the default implementation.
Execution Sample
Kotlin
interface Greeter {
    fun greet() {
        println("Hello from default!")
    }
}

class Friendly : Greeter {
    override fun greet() {
        println("Hi there!")
    }
}

fun main() {
    val f = Friendly()
    f.greet()
}
This code defines an interface with a default greet method, a class that overrides it, and calls greet on the class instance.
Execution Table
StepActionEvaluationOutput
1Define interface Greeter with default greet()Interface created with default greet()
2Define class Friendly implementing GreeterClass Friendly ready, overrides greet()
3Create instance f of Friendlyf is Friendly instance
4Call f.greet()Check if Friendly overrides greet() -> YesHi there!
5End of main()Program ends
💡 Program ends after calling overridden greet() method on Friendly instance
Variable Tracker
VariableStartAfter Step 3Final
fundefinedFriendly instance createdFriendly instance
Key Moments - 2 Insights
Why does calling greet() on Friendly print "Hi there!" instead of the default message?
Because Friendly overrides the greet() method, so the call uses the class's version, as shown in execution_table step 4.
What happens if Friendly does not override greet()?
The default greet() from the interface is used, printing "Hello from default!". This is the fallback behavior if no override exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what output is produced when f.greet() is called?
A"Hello from default!"
BNo output
C"Hi there!"
DCompilation error
💡 Hint
Check the Output column at step 4 in the execution_table.
If the class Friendly did not override greet(), what would be the output at step 4?
A"Hi there!"
B"Hello from default!"
CNo output
DRuntime error
💡 Hint
Refer to key_moments answer 2 about default method usage.
In variable_tracker, what is the value of variable 'f' after step 3?
AFriendly instance
Bnull
Cundefined
DGreeter instance
💡 Hint
Look at variable_tracker row for 'f' after step 3.
Concept Snapshot
interface InterfaceName {
    fun method() { // default implementation
        // code
    }
}

class MyClass : InterfaceName {
    override fun method() { // optional override
        // code
    }
}

- Interfaces can have methods with default code.
- Classes can override these methods or use defaults.
- Calling the method uses override if present, else default.
Full Transcript
This example shows how Kotlin interfaces can have default method implementations. The interface Greeter defines greet() with a default print statement. The class Friendly implements Greeter and overrides greet() with its own print statement. When we create an instance of Friendly and call greet(), the overridden method runs, printing "Hi there!". If Friendly did not override greet(), the default method from the interface would run instead. This allows interfaces to provide common behavior while letting classes customize it. The execution table traces each step: defining interface and class, creating instance, calling method, and output. The variable tracker shows the instance creation. Key moments clarify why the overridden method runs and what happens without override. The quiz tests understanding of output and variable states. The snapshot summarizes syntax and behavior for quick reference.