Use interface where possible
Last modified: 08 March 2021
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.
On the main menu or on 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.
Example
Before | After |
---|---|
|
|
|
|
|
|