Why in Kotlin companion objects can implements som...
# announcements
r
Why in Kotlin companion objects can implements some interfaces, what benefits this can have? When is useful to use this feature?
g
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)
r
Can you please provide detailed use cases?
c
See how kotlin-logging adds
logger
to the class using companion objects.
r
Isn't it possible to implement using annotations?
g
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