Delegation instead of inheritance in singleton


I would like to inherit the functionality of an object into another, but this is not possible.

Then I decided to give it a try using delegation instead of direct inheritance. The compiler is not complaining for what I have done!
Below you can find more info:

  1. There is an interface ThrottleBrake (implementation)
  2. There is a singleton ThrottleBrakeImpl which extends the interface above (implementation)
  3. There is another singleton ElectronicThrottleBrakeImpl which uses both of the above as it is shown in the next code snippet:
object ElectronicThrottleBrakeImpl: ThrottleBrake by ThrottleBrakeImpl {

override fun throttle(direction: String, value: Int): String {
    return ThrottleBrakeImpl.throttle(direction, value)

Is this implementation a good practice? Is the language designed to do that, or is it a side-effect of the main design?


If the compiler thinks it is OK, it is OK. It seems that everything is legal. Normally, delegating to the class does not work, but since ThrottleBrakeImpl is singleton object, it should work.

The implementation delegation is considered to be one of kotlin features which are not loved by kotlin developers, but I think it is not because it is discouraged, but because it is hard to maintain. You can use it from time to time, but you should remember that under the hood, implementation delegation creates a lot of additional bytecode, so it could increase distribution size and load times. It is better to stick to conservative inheritance wherever it is possible.


@darksnake I suspect the unenthusiasm with the current method of delegation has more to do with the language surface being as large as it is, instead of it being more of an API surface (if I’m remembering correctly, believe I got this idea from here)

@BILLyTheLiTTle Your code works as I would expect. Your delegation meets these requirements:

  • You must be extending an interface
  • You must be delegated to an object of that interface

In your example, ThrottleBrake is the interface and ThrottleBreakImpl is the object.
Of course, you could have used any other object, but a singleton object is just fine.