Extract superclass
The Extract Superclass refactoring lets you create a superclass for an existing class or rename the original class, so that it becomes an implementation for the newly created superclass. In this case, IntelliJ IDEA changes all original class usages to use a superclass where possible.
The members of the original class can be moved to the superclass. For a method, you can transfer only the method declaration but not the implementation, declaring the method as abstract in the superclass. As a result, you will have a superclass and the original class inheriting from the superclass.
Open the class in the editor and from the main menu select
.In the dialog that opens, specify a name for your class, location and class members that you want to include to form your superclass. Select the Make abstract checkbox to leave the method implementation within the current class, and declare it abstract in the extracted superclass. Click Refactor.
Example
Before | After |
---|---|
// File Class.java
public class Class {
public int varInt;
private double varDouble;
public static final int CONSTANT = 0;
public void publicMethod() {
...
}
public void hiddenMethod() {
...
}
public void setVarDouble(double var) {
this.varDouble = var;
}
public double getVarDouble() {
return
varDouble;
}
}
|
// File Class.java
public class Class extends SuperClass {
public int varInt;
public void publicMethod() {
...
}
public void hiddenMethod() {
...
}
}
// NEW file SuperClass.java
public abstract class SuperClass {
private double varDouble;
public static final int CONSTANT = 0;
public abstract void publicMethod();
public void setVarDouble(double var) {
this.varDouble = var;
}
public double getVarDouble() {
return varDouble;
}
}
|