Replace Inheritance with Delegation
The Replace Inheritance With Delegation refactoring allows removing a class from inheritance hierarchy, while preserving the functionality of the parent. IntelliJ IDEA creates a private inner class, that inherits the former superclass or interface. Selected methods of the parent are invoked via the new inner class.
To replace inheritance with delegation, follow these steps
- Select the class to be refactored in the Project Tool Window, or open this class for editing and place the caret somewhere in the source code of the class.
- On the main menu, or on the context menu of the selection, choose .
The Replace Inheritance With Delegation dialog box opens.
- In the Replace with delegation inheritance from field, select the parent object, inheritance to which will be replaced.
- Specify the name for the field of the new inner class.
- In the Inner class name field, specify the name for the inner class definition.
- In the Delegate members area, select the members of the parent class, that will be delegated through the inner class.
- To create a getter for the inner class, select the Generate getter for delegated component check box.
- To review the intended changes and make final corrections prior to the refactoring, click Preview. To apply the changes immediately, click Refactor.