Example
| Before | After |
|---|---|
// File Class.java public class Class implements Interface { public void publicMethod() { ... } public void hiddenMethod() { ... } } |
// File Class.java UNCHANGED public class Class implements Interface { public void publicMethod() { ... } public void hiddenMethod() { ... } } |
// File Interface.java public interface Interface { int CONSTANT=0; void publicMethod(); } |
// File Interface.java UNCHANGED public interface Interface { int CONSTANT=0; void publicMethod(); } |
// File AnotherClass.java public class AnotherClass { Class firstClass; Class secondClass; public void method() { firstClass.publicMethod(); firstClass.hiddenMethod(); secondClass.publicMethod(); if (secondClass instanceof Class) { ... } ... } } |
// File AnotherClass.java MODIFIED public class AnotherClass { Class firstClass; Interface secondInterface; public void method() { firstClass.publicMethod(); firstClass.hiddenMethod(); secondInterface.publicMethod(); if (secondInterface instanceof Interface) { ... } ... } } |
- 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 one of the views (Project, Commander).
- 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, which 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
If you use instanceof statements and leave this check box unselected, you may receive errouneous 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 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 appears now.

