CLion 2016.3 Help

Extract Subclass

Basics

The Extract Subclass refactoring enables extracting certain members of a class into a subclass.

Example

BeforeAfter
/help/img/idea/2016.3/cl_extractSubclassBefore.png
/help/img/idea/2016.3/cl_extractSubclassAfter.png

To extract a Subclass

  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 | Subclass.
  3. In the dialog box that appears, specify the following information:
    • Name of the new subclass.
    • Members to be included in the subclass.
      /help/img/idea/2016.3/cl_extractSubclassDialog.png
  4. Proceed with the refactoring.

Extracting the class dependent members

Let's consider the following sample of code:
/help/img/idea/2016.3/cl_extractSubclassError1.png
As you can see, variable fv here depends on static constant d2. If you apply Extract Subclass refactoring to the above class and try to select d2 to be moved, and leave fv in the initial class, CLion highlights the problem member in Extract Subclass dialog, as following:
/help/img/idea/2016.3/cl_extractSubclassError2.png

Trying to proceed with extract, you will get the following warning message:

/help/img/idea/2016.3/cl_extractSubclassError3.png

Choose Continue to ignore the problem and proceed with refactoring, or Cancel to return back and resolve it. Also you can observe the conflict in Find Tool Window.

See Also

Last modified: 29 March 2017