Here’s Why You Should Replace Inheritance with Delegation

Replace restrictive inheritance hierarchies with flexible object delegation.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Transform your rigid inheritance into flexible delegations

TL;DR: Replace restrictive inheritance hierarchies with flexible object delegation

Problems Addressed 🤯

  • Liskov substitution violation
  • Rigid class hierarchy
  • Hidden dependencies
  • Tight Coupling
  • Limited Reusability
  • Single Responsibility principle violation

Related Code Smells 🧑‍💻

https://hackernoon.com/code-smell-290-refused-bequest?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-iii-t7h3zkv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xiv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vii-8dk31x0

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

Steps 🔄

  1. Create a temporary field in the subclass for the superclass.
  2. Update subclass methods to delegate calls.
  3. Add delegation methods for inherited behavior.
  4. Remove inheritance and update object creation.

Sample Code 💻

Before 🚨

class Chatbot {    
    public void respond(String question) {
        // Here is the logic to answer a question
    }
}

class Robot extends Chatbot {
    // The Physical Robot inherits the logic
    // to answer questions
    // and adds physical behavior
    public void move() {
        System.out.println("Moving...");
    }

    public void grab() {
        System.out.println("Grabbing object...");
    }
}

After

class Brain {
    public String answer(String question) {
        // The common logic to answer questions
        // is extracted into a different object
        return "Thinking... Answering: " + question;
    }
}

final class Chatbot {    
    private final Brain brain;

    Chatbot(Brain brain) {
        this.brain = brain;
    }

    public void respond(String question) {
        System.out.println(this.brain.answer(question));
    }
}

final class Robot {
    // 4. Remove inheritance and update object creation.
    private final Brain brain;    
    // 1. Create a temporary field in the subclass for the superclass.
    // private final Chatbot chatbot;  

    Robot(Brain brain) {
        this.brain = brain;
        // 2. Update subclass methods to delegate calls.
        // this.chatbot = new Chatbot(brain);
        // This code is removed after step 4
    }

    public void move() {
        System.out.println("Moving...");
    }

    public void grab() {
        System.out.println("Grabbing object...");
    }

    public void respond(String question) {
        // 3. Add delegation methods for inherited behavior.
        // chatbot.respond(question);
        // This code is also removed after step 4 
        System.out.println(this.brain.answer(question));
        // The physical robot can also use it as text-to-speech
    }
}

Type 🛠️

  • Semi-Automatic

Safety 🛡️

This refactoring is safe when done carefully and with proper testing.

\ You should ensure all delegated method signatures match exactly and maintain existing behavior.

\ The main risk comes from missing methods that need delegation or incorrectly implementing the delegation methods.

Why is the Code Better? ✨

You gain the flexibility to change implementations at runtime and avoid the pitfalls of inheritance like tight coupling.

How Does it Improve the Bijection?

This refactoring improves the Bijection between code and reality by better modeling real-world relationships.

\ A robot doesn't inherit from a brain in the real world - it has a brain.

\ By replacing inheritance with delegation, you create a more accurate representation of the actual relationship between objects using the MAPPER.

Limitations ⚠️

The rewriting requires writing additional delegation methods.

\ If subclass logic relies too much on the superclass, delegation might increase boilerplate.

Refactor with AI

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini | | DeepSeek | DeepSeek | | Meta AI | Meta AI |

Tags 🏷️

  • Inheritance

Related Refactorings 🔄

https://maximilianocontieri.com/refactoring-007-extract-class?embedable=true

See also 📚

https://refactoring.guru/replace-inheritance-with-delegation?embedable=true

Credits

Image by Gerd Altmann on Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2025-02-17T09:30:20+00:00) Here’s Why You Should Replace Inheritance with Delegation. Retrieved from https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/

MLA
" » Here’s Why You Should Replace Inheritance with Delegation." Maximiliano Contieri | Sciencx - Monday February 17, 2025, https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/
HARVARD
Maximiliano Contieri | Sciencx Monday February 17, 2025 » Here’s Why You Should Replace Inheritance with Delegation., viewed ,<https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Here’s Why You Should Replace Inheritance with Delegation. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/
CHICAGO
" » Here’s Why You Should Replace Inheritance with Delegation." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/
IEEE
" » Here’s Why You Should Replace Inheritance with Delegation." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/. [Accessed: ]
rf:citation
» Here’s Why You Should Replace Inheritance with Delegation | Maximiliano Contieri | Sciencx | https://www.scien.cx/2025/02/17/heres-why-you-should-replace-inheritance-with-delegation/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.