> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Interface Delegation

Kotlin interface delegation is a language-level feature that allows a class to implement an interface by forwarding all of its public member invocations to a designated backing object. Utilizing the `by` keyword, the Kotlin compiler automatically generates the necessary boilerplate to route method calls and property accesses from the implementing class to the delegate instance.

## Syntax and Mechanics

To implement interface delegation, the delegating class must declare the interface it implements, followed by the `by` keyword and the instance that will handle the implementation.

```kotlin theme={"dark"}
interface Engine {
    val cylinders: Int
    fun start()
}

class V8Engine : Engine {
    override val cylinders: Int = 8
    override fun start() = println("V8 starting")
}

// Car implements Engine by delegating to the provided 'engine' instance.
// Note the absence of 'val' or 'var', which prevents the generation of an additional property backing field.
class Car(engine: Engine) : Engine by engine
```

In this structure, `Car` is recognized by the type system as an `Engine`. When `start()` or `cylinders` is invoked on an instance of `Car`, the call is directly routed to the `engine` object passed via the primary constructor.

## Overriding Delegated Members and the `this` Context

A delegating class retains the ability to provide its own implementation for specific interface members. When a member is explicitly overridden in the delegating class, the compiler uses the local implementation instead of forwarding the call to the delegate.

```kotlin theme={"dark"}
interface Engine {
    fun start()
    fun rev()
}

class V8Engine : Engine {
    override fun start() {
        println("V8 starting")
        rev() // Internal call to rev()
    }
    
    override fun rev() = println("V8 revving")
}

class CustomCar(engine: Engine) : Engine by engine {
    // Overrides the delegate's method
    override fun rev() = println("CustomCar revving")
}
```

**Critical Behavioral Detail:** The delegate object maintains its own `this` context. If the delegate object internally calls another method of the interface, it will invoke its own implementation, not the overridden version provided by the delegating class. In the example above, calling `CustomCar(V8Engine()).start()` will print `"V8 revving"`, not `"CustomCar revving"`. This is a fundamental difference between delegation and inheritance.

Additionally, when overriding a delegated member, the delegating class cannot access the delegate's implementation using the `super` keyword. The delegate is strictly a backing field, not a superclass.

## Multiple Delegation

Kotlin supports delegating multiple interfaces within a single class declaration. Each interface must be delegated to a distinct instance that implements that specific interface.

```kotlin theme={"dark"}
interface Transmission {
    fun shift()
}

class AutoTransmission : Transmission {
    override fun shift() = println("Shifting")
}

class AdvancedVehicle(
    engine: Engine,
    transmission: Transmission
) : Engine by engine, Transmission by transmission
```

## Compiler Behavior (Under the Hood)

Interface delegation is a compile-time mechanism. The Kotlin compiler translates the `by` keyword into standard JVM bytecode equivalent to the manual implementation of the Delegation pattern.

For the `Car` class defined earlier (declared with `engine: Engine` rather than `val engine: Engine`), the compiler generates a single private, synthetic backing field for the delegate and creates proxy methods for every interface member:

```java theme={"dark"}
// Conceptual Java equivalent of the generated bytecode
public final class Car implements Engine {
    // Synthetic backing field generated by the compiler
    private final Engine $$delegate_0;

    public Car(Engine engine) {
        this.$$delegate_0 = engine;
    }

    @Override
    public int getCylinders() {
        return this.$$delegate_0.getCylinders();
    }

    @Override
    public void start() {
        this.$$delegate_0.start();
    }
}
```

## Instantiation Requirements

The object acting as the delegate must be initialized and available at the time of the delegating class's instantiation. It is most commonly passed through the primary constructor, but it can also be a directly instantiated object or a companion object, provided it conforms to the target interface.

```kotlin theme={"dark"}
// Delegating to a directly instantiated object rather than a constructor parameter
class DefaultCar : Engine by V8Engine()
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
