0
0
Kotlinprogramming~10 mins

Companion object with interfaces in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Companion object with interfaces
Define Interface
Create Class with Companion Object
Companion Object Implements Interface
Call Interface Method via Companion Object
Execute Method Logic
Output Result
This flow shows how a companion object inside a class can implement an interface and how its methods are called.
Execution Sample
Kotlin
interface Greeter {
    fun greet(): String
}

class Person {
    companion object : Greeter {
        override fun greet() = "Hello from companion!"
    }
}

fun main() {
    println(Person.greet())
}
This code defines an interface, a class with a companion object implementing it, and calls the greet method.
Execution Table
StepActionEvaluationResult
1Define interface Greeter with greet()Interface createdGreeter interface ready
2Define class Person with companion object implementing GreeterCompanion object createdPerson.Companion implements Greeter
3Override greet() in companion objectMethod greet() returns fixed stringgreet() returns "Hello from companion!"
4Call Person.greet() in mainCalls companion object's greet()Returns "Hello from companion!"
5Print returned stringOutput to consoleHello from companion!
6Program endsNo more instructionsExecution stops
💡 Program ends after printing the greeting from companion object
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Person.CompanionNot createdCreated as Greeter implementergreet() method definedgreet() method calledNo change
Key Moments - 2 Insights
Why can we call greet() directly on Person without creating an instance?
Because greet() is defined inside the companion object which acts like a static member; see execution_table step 4 where Person.greet() calls the companion's method.
How does the companion object implement the interface?
The companion object declares it implements the interface and overrides its methods, as shown in execution_table step 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does step 4 represent?
ACalling the greet() method on the companion object
BDefining the interface Greeter
CCreating the Person class
DPrinting the greeting
💡 Hint
Check the 'Action' and 'Evaluation' columns in step 4 of execution_table
At which step is the greet() method overridden in the companion object?
AStep 1
BStep 5
CStep 3
DStep 2
💡 Hint
Look for 'Override greet()' in the 'Action' column of execution_table
If we remove the interface implementation from the companion object, what changes in the execution?
AProgram will print the same greeting
BPerson.greet() call will fail because greet() is not defined
CCompanion object will still implement Greeter
DMain function will not run
💡 Hint
Refer to variable_tracker and execution_table steps 2 and 4 about interface implementation
Concept Snapshot
Companion object can implement interfaces.
Define interface with methods.
Companion object declares interface implementation.
Override interface methods inside companion.
Call methods via ClassName.method() without instance.
Useful for static-like behavior with interface contracts.
Full Transcript
This example shows how a Kotlin companion object inside a class can implement an interface. First, the interface Greeter is defined with a greet() method. Then, the class Person has a companion object that implements Greeter and overrides greet() to return a greeting string. In main, calling Person.greet() calls the companion object's greet method directly, printing the greeting. The companion object acts like a static member but can follow interface contracts. This allows calling interface methods without creating class instances.