Delegation
Interface delegation with by, delegated properties, and override precedence.
4 questions
JuniorTheoryVery commonWhat does interface delegation with by do in Kotlin?
What does interface delegation with by do in Kotlin?
class B(p: I) : I by p makes B implement I by forwarding every I method to the object p. The compiler generates the forwarding methods, so B reuses p's behaviour without writing each call — composition over inheritance.
Common mistakes
- ✗Reading
byas inheritance rather than method forwarding to a held object - ✗Confusing interface delegation with property delegation like
by lazy - ✗Thinking the delegate
pis copied rather than referenced
Follow-up questions
- →What happens if the delegating class also declares its own
overrideof a method? - →Why is delegation often preferred over inheritance for code reuse?
JuniorTheoryCommonWhat does by lazy defer, and what are its thread-safety modes?
What does by lazy defer, and what are its thread-safety modes?
by lazy defers its initializer to the property's first read, then caches the result for every later read. Its default mode SYNCHRONIZED locks so exactly one thread runs the initializer; PUBLICATION may run it in several threads but publishes the first result; NONE drops synchronization.
Common mistakes
- ✗Thinking
by lazyre-runs its initializer on every read instead of caching - ✗Assuming
NONEis the default rather thanSYNCHRONIZED - ✗Confusing
by lazywithlateinit, which defers assignment but never caches a computation
Follow-up questions
- →When is it safe to drop to
LazyThreadSafetyMode.NONE? - →Why can
by lazyback avalwhilelateinitcannot?
MiddleTheoryOccasionalHow does a property delegate work, and what does the stdlib delegate Delegates.observable add?
How does a property delegate work, and what does the stdlib delegate Delegates.observable add?
var x: T by d rewrites every read as d.getValue(thisRef, property) and every write as d.setValue(thisRef, property, value), so any object declaring those operator functions is a delegate. Delegates.observable holds the value and calls your callback after each write with the property, the old value and the new.
Common mistakes
- ✗Thinking a delegate is dispatched reflectively rather than by the
operatorconvention - ✗Expecting
Delegates.observableto fire before the write and be able to veto it - ✗Believing only a
ReadWritePropertysubclass may serve as a delegate
Follow-up questions
- →Which stdlib delegate does let you reject a write, and how does it signal that?
- →What does the
KPropertyargument passed togetValuegive the delegate access to?
MiddleTheoryOccasionalWith interface delegation by, does an explicit override on the class win?
With interface delegation by, does an explicit override on the class win?
Yes. An override on the delegating class always wins — by supplies only the methods you do not override. Delegating one interface to two objects that share a method is an unresolved conflict: the class must override it explicitly or it won't compile.
Common mistakes
- ✗Thinking the delegate's method shadows an explicit class
override - ✗Assuming two delegates of the same interface merge without a compile error
- ✗Believing source order of the
byclauses decides the winner
Follow-up questions
- →What compile error appears when two delegated interfaces share a method signature?
- →Does the delegate object see calls made through the delegating class's own
override?