Why in Kotlin companion objects can implements som...
# announcements
Why in Kotlin companion objects can implements some interfaces, what benefits this can have? When is useful to use this feature?
Same benefits as any inheritance/implementation. You can have for example a few different classes with the same companion object contract, for example you can implement some interface with different builder methods and use this companion object in polymorphic way. It actually fixes one of
static method
problems from Java, where static methods cannot implement interface and few different classes cannot provide the same methods set (they can provide the same set, but you cannot use them without hard reference to this class or without reflections)
Can you please provide detailed use cases?
See how kotlin-logging adds
to the class using companion objects.
Isn't it possible to implement using annotations?
But companion object doesn’t require any reflections or annotation processing
Maybe this feature itself is not very wide used, but still covers some good use cases and works with OOP paradigm better than static methods