---
title: "Replace Inheritance with Delegation"
type: "refactoring-technique"
slug: "replace-inheritance-with-delegation"
url: "http://localhost:3000/en/replace-inheritance-with-delegation.md"
category: "Dealing with Generalization"
description: "Problem: You have a subclass that uses only a portion of the methods of its superclass (or it’s not possible to inherit superclass data). Solution: Create a field and put a superclass object in it, delegate methods to the superclass object, and get rid of inheritance."
---
# Replace Inheritance with Delegation

> Problem: You have a subclass that uses only a portion of the methods of its superclass (or it’s not possible to inherit superclass data). Solution: Create a field and put a superclass object in it, delegate methods to the superclass object, and get rid of inheritance.

## Problem

You have a subclass that uses only a portion of the methods of its superclass (or it’s not possible to inherit superclass data).

## Solution

Create a field and put a superclass object in it, delegate methods to the superclass object, and get rid of inheritance.

## Why Refactor

Replacing inheritance with composition can substantially improve class design if:

* Your subclass violates the _Liskov substitution principle_, i.e., if inheritance was implemented only to combine common code but not because the subclass is an extension of the superclass.
* The subclass uses only a portion of the methods of the superclass. In this case, it’s only a matter of time before someone calls a superclass method that he or she wasn’t supposed to call.

In essence, this refactoring technique splits both classes and makes the superclass the helper of the subclass, not its parent. Instead of inheriting all superclass methods, the subclass will have only the necessary methods for delegating to the methods of the superclass object.

## Benefits

* A class doesn’t contain any unneeded methods inherited from the superclass.
* Various objects with various implementations can be put in the delegate field. In effect you get the [Strategy](/design-patterns/strategy) design pattern.

## How to Refactor

1. Create a field in the subclass for holding the superclass. During the initial stage, place the current object in it.
2. Change the subclass methods so that they use the superclass object instead of `this`.
3. For methods inherited from the superclass that are called in the client code, create simple delegating methods in the subclass.
4. Remove the inheritance declaration from the subclass.
5. Change the initialization code of the field in which the former superclass is stored by creating a new object.
## Relations

**Opposite refactorings**

- [Replace Delegation with Inheritance](/en/replace-delegation-with-inheritance.md)

