# Use interface where possible

> **TL;DR**
> `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.

Procedure:

1. In the editor, place the caret at a class which methods should be delegated to its parent class or interface.

2. From the main menu or from the context menu, select `Refactor | Use Interface Where Possible`.

3. In the dialog that opens, select the parent object that will replace the usages of the current class.

4. 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:

```JAVA
if (secondInterface instanceof Class)
```

This code will compile, but may produce undesired results.

5. [Preview](refactoring-source-code.html#changes_preview) and apply the changes.

## Example

| Before | After |
| --- | --- |
|     ```JAVA // File Class.java public class Class implements Interface {     public void publicMethod() {         ...     }     public void hiddenMethod() {         ...     } } ```    |     ```JAVA // File Class.java UNCHANGED public class Class implements Interface {     public void publicMethod() {         ...     }     public void hiddenMethod() {         ...     } } ```    |
|     ```JAVA // File Interface.java public interface Interface {     int CONSTANT=0;     void publicMethod(); } ```    |     ```JAVA // File Interface.java UNCHANGED public interface Interface {     int CONSTANT=0;     void publicMethod(); } ```    |
|     ```JAVA // File AnotherClass.java public class AnotherClass {     Class firstClass;     Class secondClass;     public void method() {         firstClass.publicMethod();         firstClass.hiddenMethod();         secondClass.publicMethod();         if (secondClass instanceof Class) {             ...         }         ...     } } ```    |     ```JAVA // File AnotherClass.java MODIFIED public class AnotherClass {     Class firstClass;     Interface secondInterface;     public void method() {         firstClass.publicMethod();         firstClass.hiddenMethod();         secondInterface.publicMethod();         if (secondInterface instanceof Interface) {             ...         }         ...     } } ```    |

## Use interface where possible dialog

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 `instanceof`  | If this option is checked, IntelliJ IDEA scans the source code for the presence of the `instanceof` statements for the selected superclass or interface, and change it, if found.  |

## See also

### Refactoring

[Code refactoring](refactoring-source-code.html)

