IntelliJ IDEA 2016.3 Help

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

BeforeAfter
// 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 { Int; blicMethod() { ddenMethod() { lass.java ass SuperClass { varDouble; final int CONSTANT = 0; t void publicMethod(); tVarDouble(double var) { uble = var; getVarDouble() { Double;

To extract a superclass

  1. Select the desired class in one of the views, or just open it in the editor.
  2. On the on the main menu or on the context menu, choose Refactor | Extract | Superclass.
  3. 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 leave it where it is, select the As is option.
      • To copy it to the extracted superclass, select the Copy option.
      • To move it to the extracted superclass, select the Move option.
  4. 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.

See Also

Last modified: 7 December 2016