Replace inheritance with delegation
Last modified: 10 August 2022The Replace Inheritance With Delegation refactoring lets you remove 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.
Select a class you want to refactor.
On the main menu, or on the context menu, select Refactor | Replace Inheritance With Delegation.
In the dialog that opens, specify parent object, the name of the inner class definition. Also, select the members of the parent class that will be delegated through the inner class, and getter options.
Preview and apply changes.
Example
Before | After |
---|---|
|
|
Thanks for your feedback!
Was this page helpful?