Deep Kotlin tutorial covering composition, delegation, by, repository interface with examples, diagram, mistakes, and interview answers.

Composition and Delegation

What is Composition and Delegation?

Composition and Delegation is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: composition, delegation, by, repository interface, testability. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Composition and Delegation in simple language
  • Use composition in real Kotlin or Android code
  • Explain Composition and Delegation in interviews with examples and trade-offs

How it works

Composition and Delegation: composition, delegation, by, repository interface, testability works by shaping responsibilities. Kotlin lets you combine OOP modeling with functional transformations: use objects for behavior and boundaries, then use pure functions for predictable data changes.

  1. Identify what problem Composition and Delegation: composition, delegation, by, repository interface, testability solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • composition: core concept in this lesson
  • delegation: core concept in this lesson
  • by: core concept in this lesson
  • repository interface: core concept in this lesson
  • testability: core concept in this lesson
  • Class: blueprint that owns state and behavior
  • Interface: a contract with no state of its own
  • Inheritance: reusing behavior through an is-a relationship

Important differences

Concept 1Concept 2How to remember it
InheritanceCompositionInheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
InterfaceAbstract classAn interface is a pure contract with no state; an abstract class can hold shared state and partial implementation.
Data classSealed classA data class models one shape of data; a sealed class models a closed set of alternative shapes, useful for representing UI state.

Diagram

flowchart TD
A["1.5 Object-Oriented vs Functional Kotlin"] --> B["Composition and Delegation"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

Sealed class for UI state

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}

Delegation instead of inheritance

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source

When to use it

Use it when: Use this to model real domain concepts clearly - a data class for a User or Order, a sealed class for a finite set of UI states, an interface for a contract that several classes share.

Avoid it when: Avoid deep inheritance hierarchies for code reuse alone; prefer composition when the relationship is not a genuine is-a relationship.

Common mistakes

  • Reaching for inheritance when composition would be more flexible
  • Building deep inheritance hierarchies that are hard to change safely
  • Putting mutable shared state directly on an open base class
  • Using a plain class where a sealed class would make invalid states unrepresentable
  • Skipping interfaces and coding directly against a concrete implementation, which hurts testability

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is Composition and Delegation in 1.5 Object-Oriented vs Functional Kotlin?

Answer: Composition and Delegation is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: composition, delegation, by, repository interface, testability. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

2. How does Composition and Delegation work in Kotlin or Android code?

Answer: Composition and Delegation: composition, delegation, by, repository interface, testability works by shaping responsibilities. Kotlin lets you combine OOP modeling with functional transformations: use objects for behavior and boundaries, then use pure functions for predictable data changes.

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Building deep inheritance hierarchies that are hard to change safely

3. What is one common mistake with Composition and Delegation?

Answer: A common mistake is: Reaching for inheritance when composition would be more flexible Another is: Building deep inheritance hierarchies that are hard to change safely

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

4. How would you explain Composition and Delegation with a small example?

Answer: Composition and Delegation is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: composition, delegation, by, repository interface, testability. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Building deep inheritance hierarchies that are hard to change safely

5. What is the trade-off of using Composition and Delegation?

Answer: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

Practice task

Write one Kotlin example for Composition and Delegation. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.