Also known as

Refactoring Introduce Local Extension


A utility class does not contain some methods that you need. But you cannot add these methods to the class.


Create a new class containing the methods and make it either the child or wrapper of the utility class.
Introduce Local Extension - Before
Introduce Local Extension - After

Why Refactor

The class that you are using does not have the methods that you need. What's worse, you cannot add these methods (because the classes are in a third-party library, for example).

There are two ways out:

  • Create a subclass from the relevant class, containing the methods and inheriting everything else from the parent class. This way is easier but is sometimes blocked by the utility class itself (due to final).
  • Create a wrapper class that contains all the new methods and elsewhere will delegate to the related object from the utility class. This method is more work since you need not only code to maintain the relationship between the wrapper and utility object, but also a large number of simple delegating methods in order to emulate the public interface of the utility class.


  • By moving additional methods to a separate extension class (wrapper or subclass), you avoid gumming up client classes with code that does not fit. Program components are more coherent and are more reusable.

How to Refactor

  1. Create a new extension class:
  • Option A: Make it a child of the utility class.
  • Option B: If you have decided to make a wrapper, create a field in it for storing the utility class object to which delegation will be made. When using this option, you will need to also create methods that repeat the public methods of the utility class and contain simple delegation to the methods of the utility object.
  1. Create a constructor that uses the parameters of the constructor of the utility class.

  2. Also create an alternative "converting" constructor that takes only the object of the original class in its parameters. This will help to substitute the extension for the objects of the original class.

  3. Create new extended methods in the class. Move foreign methods from other classes to this class or else delete the foreign methods if their functionality is already present in the extension.

  4. Replace use of the utility class with the new extension class in places where its functionality is needed.

Tired of reading?

No wonder, there are 7 hours worth of the text on this website.

Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.

Learn more...

Live Example

First time here? No worries!

Everything is quite simple. This example is like video (but much cooler):

  1. After pressing the Play button, you will see floating messages, which will guide you through the example. You can proceed by clicking on them.
  2. You can fast-forward or return to previous steps by pressing arrow buttons on the left.
  3. Also, you can take a look at the code diff window, which will show what has changed in code during the example ()
  4. To check the code for errors, you can press "Compile and test" button ()