Use interface where possible
Refactor | Use Interface Where Possible
Use Interface Where Possible refactoring delegates execution of the specified methods, derived from a base class/interface, to an instance of an ancestor class or an inner class, implementing the same interface.
In the editor, place the caret at a class which methods should be delegated to its parent class or interface.
From the main menu or from the context menu, select Refactor | Use Interface Where Possible.
In the dialog that opens, select the parent object that will replace the usages of the current class.
To replace the current class name in
instanceof
statements, check the option Use interface/superclass in instanceof.Note that if you use
instanceof
statements and leave this checkbox unselected, you may receive erroneous code, such as:if (secondInterface instanceof Class)
This code will compile, but may produce undesired results.
Preview and apply the changes.
Before | After |
---|---|
|
|
|
|
|
|
This dialog appears when you invoke the Use interface where possible refactoring.
Item | Description |
---|---|
Change usages | Select the parent class or interface, which will replace the usages of the current class. |
Use interface/superclass in | If this option is checked, IntelliJ IDEA scans the source code for the presence of the |
Thanks for your feedback!