Extract Superclass
Basics
The Extract Superclass refactoring has two options:
- Create a superclass based on an existing class.
- 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.
Fields and methods in the original class can be moved to the superclass. Also 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 the superclass and the original class inherited from the superclass.
Example
To extract a superclass
- Select the desired class in one of the views, or just open it in the editor.
- On the on the main menu or on the context menu, choose .
- In the Extract Superclass dialog box:
- To create a new superclass, select the Extract superclass option and specify the name for the new superclass.
To rename the original class and make it an implementation of the newly created superclass, select the Rename original class and use superclass where possible option and specify the new name for the original class. IntelliJ IDEA will change all original class usages to the usages of the implementing class only where it is still necessary.
- Specify the package, where the new superclass will be located.
- In the Members to Form Superclass area, select the class members you want to
be moved or delegated to the superclass.
To leave the method implementation within the current class, and declare it abstract in the extracted superclass, select the Make Abstract check box.
- In the JavaDoc area, select the action to be applied on the JavaDoc.
- To create a new superclass, select the Extract superclass option and specify the name for the new superclass.
- Click Refactor to proceed.
If the class is used within the current project, IntelliJ IDEA proposes to replace the instances of this class with the instances of the new superclass. If a class instance references a member, which is not defined in the superclass, it won't be suggested for replacement.