IntelliJ IDEA 14.1.0 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

Before After
// File Class.java ublic class Class { arInt; arDouble; inal int ONSTANT = 0; licMethod() { denMethod() { VarDouble(double var) { ble = var; etVarDouble() { arDouble;
// File Class.java ublic class Class extends SuperClass { arInt; licMethod() { denMethod() { lass.java ublic abstract class SuperClass { arDouble; inal int ONSTANT = 0; void publicMethod(); VarDouble(double var) { ble = var; etVarDouble() { arDouble;

To extract a superclass

  1. Select the desired class in one of the views, or just open it in the editor.
  2. 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: 24 March 2015
comments powered by Disqus