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.
To use interface where possible, follow these steps
- Select a class whose methods should be delegated to its parent class or interface. To do that, place the caret on this class in the editor or in the Project Tool Window.
- On the main menu or on the context menu of the selection, choose .
- In the Use Interface Where Possible dialog box, select the parent object that will replace the usages of the current class.
- To replace the current class name in
instanceofstatements, check the option Use interface/superclass in instanceof.
Note that if you use
instanceofstatements 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.
- To review the intended changes and make final corrections prior to the refactoring, click Preview. To continue without preview, click Refactor.
The Rename variables dialog box appears. It lists the occurrences of the class that may be replaced by the superclass or the interface selected. Select the usages you want to replace, and (optionally) specify new names for each of them.
- Click OK to continue. If you have previously clicked the Preview button, the Preview window will appear now.