📖 Scenario: Imagine you are creating a simple system to manage user roles in an app. You want to use Kotlin's companion objects to hold some shared behavior and implement an interface to ensure consistency.
🎯 Goal: Build a Kotlin class with a companion object that implements an interface. The companion object will provide a method to describe the user role.
📋 What You'll Learn
Create an interface called RoleDescription with a function describeRole() that returns a String.
Create a class called User with a companion object.
Make the companion object implement the RoleDescription interface.
Implement the describeRole() function inside the companion object to return the string "This is a user role.".
Print the result of calling User.describeRole().
💡 Why This Matters
🌍 Real World
Companion objects with interfaces help organize shared behavior and constants related to a class, useful in many Kotlin applications like Android development.
💼 Career
Understanding companion objects and interfaces is important for Kotlin developers to write clean, reusable, and well-structured code.
Progress0 / 4 steps
1
Create the interface RoleDescription
Create an interface called RoleDescription with a function describeRole() that returns a String.
Kotlin
Hint
An interface defines a function signature without implementation.
2
Create the class User with a companion object
Create a class called User with an empty companion object.
Kotlin
Hint
A companion object is declared inside a class using the keyword 'companion object'.
3
Make the companion object implement RoleDescription and implement describeRole()
Make the companion object inside User implement the RoleDescription interface. Implement the describeRole() function to return the string "This is a user role.".
Kotlin
Hint
Use ': RoleDescription' after 'companion object' and override the function with the exact return string.
4
Print the result of calling User.describeRole()
Write a print statement to display the result of calling User.describeRole().
Kotlin
Hint
Call the function on the class name and print the result.
Practice
(1/5)
1. What is the main purpose of a companion object in Kotlin?
easy
A. To create multiple instances of a class
B. To inherit from multiple classes
C. To define instance variables for each object
D. To hold static-like members that belong to the class, not instances
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 D
Quick Check:
Companion object = static-like members [OK]
Hint: Companion object = static members inside class [OK]
Common Mistakes:
Thinking companion object creates instances
Confusing companion object with instance variables
Believing companion object can inherit multiple classes
2. Which of the following is the correct syntax to declare a companion object implementing an interface MyInterface inside a class MyClass?
easy
A. class MyClass { object companion : MyInterface { } }
B. class MyClass { companion object : MyInterface { } }
C. class MyClass { companion MyInterface object { } }
D. class MyClass { companion object implements MyInterface { } }
Solution
Step 1: Recall companion object syntax
The correct syntax is companion object followed 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 B
Quick Check:
companion object : InterfaceName [OK]
Hint: Use 'companion object : Interface' syntax [OK]
Common Mistakes:
Using 'implements' keyword instead of ':'
Swapping 'object' and 'companion' keywords
Omitting the colon before interface name
3. Given the code below, what will be printed when MyClass.printMessage() is called?
interface Printer {
fun printMessage()
}
class MyClass {
companion object : Printer {
override fun printMessage() {
println("Hello from companion")
}
}
}
fun main() {
MyClass.printMessage()
}
5. You want to create a Kotlin class 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
}
}
hard
A. Call Operations.add(5, 3)
B. Create instance and call Calculator().add(5, 3)
C. Call Calculator.add(5, 3)
D. Call Calculator.companion.add(5, 3)
Solution
Step 1: Understand companion object access
Companion object members can be called directly on the class name like Calculator.add() (syntactic sugar) or explicitly via Calculator.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 C
Quick Check:
Class.method() = companion access [OK]
Hint: Companion object methods called on class directly [OK]
Common Mistakes:
Using lowercase 'companion' instead of 'Companion'
Creating instance unnecessarily
Trying to call interface name directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.