In Kotlin, a companion object can implement interfaces, allowing you to define common behavior in a static-like manner but with the flexibility and power of object-oriented design. This feature can be particularly useful when you want your class to have a static part that adheres to a specific contract defined by an interface. Here’s how you can implement it:

Interface Definition

First, let's define an interface that we want our companion object to implement. For example, suppose we have an interface JsonFactory for creating objects from JSON strings.


interface JsonFactory<T> { fun fromJson(json: String): T }

Class with Companion Object Implementing the Interface

Now, we'll create a class that has a companion object implementing this interface. Let's assume we have a User class, and we want to be able to create User instances from JSON strings.


class User(val name: String, val age: Int) {
    companion object : JsonFactory<User> {
        override fun fromJson(json: String): User {
            // Assuming JSON parsing is done here. This is a simplified example.
            val name = json.substringAfter("name:").substringBefore(",")
            val age = json.substringAfter("age:").toInt()
            return User(name, age)
        }
    }
}

Usage

With the setup above, we can now use the companion object to create User instances from JSON strings in a static-like manner, without needing an instance of the User class.


fun main() {
    val json = "name:John Doe,age:30"
    val user = User.fromJson(json)
    println("Name: ${user.name}, Age: ${user.age}")
}

Explanation

This pattern is particularly useful in scenarios where you need a standardized way to create instances of a class from certain types of data (like JSON, XML, etc.) and want to ensure that your class adheres to a specific creation contract or pattern defined by an interface.