IntelliJ IDEA 14.1.0 Help

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.

Example

Before After
// File Class.java ublic class Class extends SuperClass { arInt; openMethod() { ass.java ublic abstract class SuperClass { c final int ONSTANT=0; act void openMethod(); secretMethod() {
// File Class.java ublic class Class { arInt; l MySuperClass superClass = new MySuperClass(); Class getSuperClass() { uperClass; openMethod() { ss.openMethod(); s MySuperClass extends SuperClass { oid openMethod() { ass.java UNCHANGED ublic abstract class SuperClass { c final int ONSTANT=0; act void openMethod(); secretMethod() {

To replace inheritance with delegation

  1. Select the class.
  2. Open the Replace Inheritance With Delegation dialog box by choosing Refactor | Replace Inheritance With Delegation on the main menu, or on the context menu of the selection.
  3. In the Replace with delegation inheritance from field, select the parent object, inheritance to which will be replaced.
  4. Specify the name for the field of the new inner class.
  5. In the Inner class name field, specify the name for the inner class definition.
  6. In the Delegate members area, select the members of the parent class, that will be delegated through the inner class.
  7. To create a getter for the inner class, select the Generate getter for delegated component check box.
  8. To review the intended changes and make final corrections prior to the refactoring, click Preview. To apply the changes immediately, click Refactor.

See Also

Last modified: 24 March 2015
comments powered by Disqus