0
0
Kotlinprogramming~10 mins

Interface declaration and implementation in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interface declaration and implementation
Declare Interface
Define Methods (no body)
Create Class
Implement Interface
Provide Method Bodies
Create Object
Call Methods
Use Implemented Behavior
First, you declare an interface with method signatures. Then, a class implements this interface by providing method bodies. Finally, you create objects and call the implemented methods.
Execution Sample
Kotlin
interface Animal {
    fun sound(): String
}

class Dog : Animal {
    override fun sound() = "Woof"
}

fun main() {
    val dog = Dog()
    println(dog.sound())
}
This code declares an interface Animal with a method sound, then a Dog class implements it, and finally prints the dog's sound.
Execution Table
StepActionEvaluationResult
1Declare interface Animal with method sound()No body, just signatureInterface Animal ready
2Create class Dog implementing AnimalMust override sound()Class Dog ready
3Override sound() in DogReturns "Woof"Method sound() implemented
4In main(), create Dog objectdog = Dog()dog object created
5Call dog.sound()Calls Dog's sound()Returns "Woof"
6Print returned valuePrints "Woof"Output: Woof
7Program endsNo more instructionsExecution stops
💡 Program ends after printing the dog's sound.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
dogundefinedDog object createdDog object used to call sound()Dog object remains
Key Moments - 3 Insights
Why does the interface method have no body?
Interfaces only declare method signatures without bodies, as shown in step 1 of the execution_table. The actual method body is provided in the implementing class (step 3).
What does 'override' mean in the class?
'override' means the class provides its own version of the interface method. Step 3 shows Dog overriding sound() to return "Woof".
Why can we call dog.sound() even though sound() is declared in the interface?
Because Dog implements the interface and provides the method body (step 3), so calling dog.sound() uses Dog's version (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling dog.sound() at step 5?
ANo output
B"Meow"
C"Woof"
DError
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 5 in the execution_table.
At which step is the Dog object created?
AStep 2
BStep 4
CStep 6
DStep 1
💡 Hint
Look for the action mentioning 'create Dog object' in the execution_table.
If the Dog class did not override sound(), what would happen when calling dog.sound()?
ACompilation error because sound() is not implemented
BIt would print "Woof" anyway
CIt would print nothing
DIt would call a default sound() from interface
💡 Hint
Interfaces require implementing all methods; see step 3 about overriding sound().
Concept Snapshot
interface InterfaceName {
    fun methodName(): ReturnType
}

class ClassName : InterfaceName {
    override fun methodName() = implementation
}

- Interfaces declare method signatures only.
- Classes must override all interface methods.
- Objects of the class use implemented methods.
Full Transcript
This visual execution shows how Kotlin interfaces work. First, an interface named Animal is declared with a method sound() that has no body. Then, a class Dog implements Animal and overrides sound() to return "Woof". In the main function, a Dog object is created and its sound() method is called, printing "Woof". The execution table traces each step from interface declaration to method call and output. Variables like dog are tracked through creation and usage. Key moments clarify why interfaces have no method bodies, the meaning of override, and how method calls work through the interface. The quiz tests understanding of method results, object creation, and implementation requirements. The snapshot summarizes syntax and rules for interface declaration and implementation in Kotlin.