100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Delegation in OOP?

Learn delegation in OOP — forwarding calls to helper objects, has-a vs is-a, and runtime flexibility — with a Java example and Q&A.

mediumQ67 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Delegation is an object-oriented technique where an object, instead of implementing a behavior itself, hands the request off to a second helper object that actually performs the work.

Rather than inheriting behavior from a superclass, a class holds a reference to another object and forwards specific method calls to it, exposing the same or a similar public method while internally invoking the helper’s implementation. This achieves code reuse without the tight coupling and fragile-base-class problems that inheritance can introduce, because the relationship is a runtime has-a association rather than a compile-time is-a hierarchy. Delegation is the mechanism underlying the "favor composition over inheritance" guideline, and it shows up explicitly in patterns like Decorator, Strategy, and Proxy. It also allows the delegate to be swapped at runtime, something static inheritance cannot do.

  • Reuses behavior without subclassing
  • Allows swapping the delegate object at runtime
  • Avoids fragile, deep inheritance hierarchies
  • Keeps classes focused via single-responsibility helpers

AI Mentor Explanation

A captain fields questions about field placement from the press but hands the actual tactical call to the bowling coach standing beside them, who has the specialized expertise. The captain still answers to the press as the public face, forwarding the real decision to the coach rather than making it personally. That is delegation: an object exposes a method to its caller but internally forwards the real work to a separate helper object it holds a reference to.

Step-by-Step Explanation

  1. Step 1

    Hold a reference to a helper

    The delegating class stores a reference to another object that implements the needed behavior.

  2. Step 2

    Expose a matching public method

    The delegating class defines a method with the same or a compatible signature.

  3. Step 3

    Forward the call

    Inside that method, the call is passed through to the helper object, which performs the actual work.

  4. Step 4

    Allow the delegate to vary

    The helper reference can be swapped at construction or at runtime to change behavior without subclassing.

What Interviewer Expects

  • A clear distinction between delegation (has-a, runtime) and inheritance (is-a, compile-time)
  • Recognition that delegation underlies composition-based design
  • Mention of at least one pattern that relies on delegation (Strategy, Decorator, Proxy)
  • Awareness that delegates can be swapped at runtime for flexible behavior

Common Mistakes

  • Confusing delegation with simple method forwarding without understanding the design intent
  • Treating delegation and inheritance as interchangeable ways to reuse code
  • Not knowing which design patterns rely on delegation
  • Assuming delegation requires a shared interface when it may not

Best Answer (HR Friendly)

Delegation means an object hands off a piece of work to another object it holds a reference to, instead of doing that work itself or inheriting it from a parent class. It is a flexible way to reuse behavior because the helper object can be swapped out at runtime, which gives more flexibility than a fixed inheritance hierarchy.

Code Example

Delegation instead of inheritance
interface FlightBehavior {
    void fly();
}

class FlyWithWings implements FlightBehavior {
    public void fly() { System.out.println("Flying with wings"); }
}

class NoFly implements FlightBehavior {
    public void fly() { System.out.println("Cannot fly"); }
}

class Duck {
    private FlightBehavior flightBehavior; // delegate reference

    Duck(FlightBehavior flightBehavior) {
        this.flightBehavior = flightBehavior;
    }

    void performFly() {
        flightBehavior.fly(); // forward the call to the delegate
    }

    void setFlightBehavior(FlightBehavior fb) {
        this.flightBehavior = fb; // swap the delegate at runtime
    }
}

Duck duck = new Duck(new FlyWithWings());
duck.performFly(); // Flying with wings
duck.setFlightBehavior(new NoFly());
duck.performFly(); // Cannot fly

Follow-up Questions

  • How does delegation differ from inheritance in terms of coupling?
  • Which design patterns rely explicitly on delegation?
  • Can delegation be combined with interfaces to achieve polymorphism?
  • Why is delegation considered safer than deep inheritance chains?

MCQ Practice

1. Delegation primarily relies on which kind of relationship between objects?

Delegation is built on a has-a (composition) relationship where one object holds a reference to a helper object.

2. Which of the following is a key advantage of delegation over inheritance?

Because delegation uses object references rather than a fixed class hierarchy, the helper can be replaced dynamically.

3. Which design pattern is a textbook example of delegation?

The Strategy pattern delegates an algorithm's execution to an interchangeable strategy object.

Flash Cards

Delegation in one line?An object forwards a method call to a helper object instead of implementing the behavior itself.

What relationship underlies delegation?has-a (composition), not is-a (inheritance).

Key runtime advantage?The delegate object can be swapped dynamically, unlike a fixed inheritance hierarchy.

Name a pattern built on delegation.Strategy, Decorator, or Proxy.

1 / 4

Continue Learning