A companion object lets you create functions or properties tied to a class, not to instances. Implementing interfaces in companion objects helps organize shared behavior or constants in one place.
Companion object with interfaces in Kotlin
class ClassName { companion object : InterfaceName { override fun interfaceMethod() { // implementation } } } interface InterfaceName { fun interfaceMethod() }
The companion object acts like a static object inside the class.
Implementing an interface in the companion object means you can call interface methods on the class itself.
interface Logger { fun log(message: String) } class MyClass { companion object : Logger { override fun log(message: String) { println("Log: $message") } } } fun main() { MyClass.log("Hello from companion") }
interface Constants { val PI: Double } class MathUtils { companion object : Constants { override val PI = 3.14159 } } fun main() { println("PI = ${MathUtils.PI}") }
This program shows a companion object implementing the Greeter interface. We call greet() on the class without creating an instance.
interface Greeter { fun greet(name: String) } class Friendly { companion object : Greeter { override fun greet(name: String) { println("Hello, $name! Welcome!") } } } fun main() { Friendly.greet("Alice") }
You can only have one companion object per class.
Companion objects can implement multiple interfaces if needed.
Use companion objects to keep related static-like code organized inside the class.
Companion objects let you create static-like members inside a class.
Implementing interfaces in companion objects helps organize shared behavior.
You can call interface methods on the class itself without creating instances.
Practice
companion object in Kotlin?Solution
Step 1: Understand companion object role
A companion object is a special object inside a class that holds members shared by all instances.Step 2: Compare with instance members
Unlike instance variables, companion object members belong to the class itself, acting like static members.Final Answer:
To hold static-like members that belong to the class, not instances -> Option DQuick Check:
Companion object = static-like members [OK]
- Thinking companion object creates instances
- Confusing companion object with instance variables
- Believing companion object can inherit multiple classes
MyInterface inside a class MyClass?Solution
Step 1: Recall companion object syntax
The correct syntax iscompanion objectfollowed by a colon and the interface name.Step 2: Check each option
class MyClass { companion object : MyInterface { } } matches the correct syntax. The other options have incorrect keywords or order.Final Answer:
class MyClass { companion object : MyInterface { } } -> Option BQuick Check:
companion object : InterfaceName [OK]
- Using 'implements' keyword instead of ':'
- Swapping 'object' and 'companion' keywords
- Omitting the colon before interface name
MyClass.printMessage() is called?interface Printer {
fun printMessage()
}
class MyClass {
companion object : Printer {
override fun printMessage() {
println("Hello from companion")
}
}
}
fun main() {
MyClass.printMessage()
}Solution
Step 1: Understand companion object implementing interface
The companion object implements Printer and overrides printMessage to print a message.Step 2: Calling printMessage on class
CallingMyClass.printMessage()calls the companion object's method, printing the message.Final Answer:
Hello from companion -> Option AQuick Check:
Companion object method called = prints message [OK]
- Expecting instance creation before method call
- Thinking printMessage is not accessible on class
- Assuming compilation error due to interface
interface Logger {
fun log(message: String)
}
class Service {
companion object Logger {
override fun log(message: String) {
println("Log: $message")
}
}
}Solution
Step 1: Check companion object declaration
The code usescompanion object Logger, which names the object but does not implement the interface.Step 2: Correct syntax for interface implementation
To implement Logger, it should becompanion object : Loggerwith a colon.Final Answer:
Companion object must use ':' to implement interface, not name after object -> Option AQuick Check:
Use ':' to implement interface in companion object [OK]
- Confusing object name with interface implementation
- Omitting colon before interface name
- Thinking companion object cannot implement interfaces
Calculator with a companion object that implements an interface Operations having a method add(a: Int, b: Int): Int. How can you call add without creating an instance of Calculator?interface Operations {
fun add(a: Int, b: Int): Int
}
class Calculator {
companion object : Operations {
override fun add(a: Int, b: Int): Int = a + b
}
}Solution
Step 1: Understand companion object access
Companion object members can be called directly on the class name likeCalculator.add()(syntactic sugar) or explicitly viaCalculator.Companion.Step 2: Check method call options
Calculator.add(5, 3)is the standard direct call. B requires an instance which doesn't have the method. C calls the interface directly (wrong). D uses incorrect casing ("companion" instead of "Companion").Final Answer:
Call Calculator.add(5, 3) -> Option CQuick Check:
Class.method() = companion access [OK]
- Using lowercase 'companion' instead of 'Companion'
- Creating instance unnecessarily
- Trying to call interface name directly
Ready to prove it?
Pass the test and this pattern moves unknown
