Extract interface
With the Extract Interface refactoring you have the following options:
Create an interface based on the methods of a class.
Rename the original class, and it implements the newly created interface. In such case, IntelliJ IDEA changes all usages of the original class to use the interface where possible.
In addition, static final fields, declared in the initial class, can be moved to an interface. As a result, an interface will be created containing the specific methods and fields. Thereby, the specified class methods become implementations of the corresponding interface methods.
Examples
Here we have a class, and perform Extract Interface refactoring to create an interface based on the methods of the class.
Before | After |
---|---|
// File AClass.java
class AClass {
public static final double CONSTANT = 3.14;
public void publicMethod() {
}
public void secretMethod() {
}
}
|
// File AClass.java
class AClass implements AnInterface {
public void publicMethod() {
}
public void secretMethod() {
}
// File AnInterface.java
public interface AnInterface {
double CONSTANT = 3.14;
void publicMethod();
}
}
|
Another example of the Extract Interface refactoring, when the Rename original class and use interface where possible option is selected.
Before | After |
---|---|
public class FormerAClass implements AClass {
public void publicMethod() {
}
public void secretMethod() {
}
}
|
public interface AClass {
double CONSTANT=3.14;
void publicMethod();
}
|
You can extract an interface from the class that already implements another interface. Let's extract interface from the class that implements AnInterface
. Depending on whether we want AnotherInterface
(extracted interface) to extend the AnInterface
(existing one) or we want source AClass
to implement them both, we will get the following code:
Extracted Interface extends the existing one:
Extracted Interface:
Source class implements both interfaces.
Source class:
Extracted Interface:
Extract an interface
Select a class in the Project view, Structure view, or place the caret anywhere within a class in the editor.
From the main menu or from the context menu of the selection, select Extract Interface dialog appears.
. TheTo extract a new interface, select the Extract Interface option and specify the name for the new interface.
To rename the original class and make it an implementation of the newly created interface, select the Rename original class and use interface where possible option and specify the new name for the original class. IntelliJ IDEA will alter all original class usages to the usages of the implementing only where it is still necessary.
Specify the package, where the new interface will be located.
Select the class members you want to be listed in the interface in the Members to form interface area. The list shows all the methods of the class, as well as final static fields (constants).
In the JavaDoc area, select the action to be applied on the JavaDoc.
To leave it where it is, select the As is option.
To copy it to the extracted interface, select the Copy option.
To move it to the extracted interface, select the Move option.
Click Refactor to proceed.
Click Refactor when ready. If IntelliJ IDEA shows you a Refactoring Preview in the Find tool window, review the suggested changes. To have the interface extracted and the proposed changes applied, click Do Refactor.