Extract superclass
Last modified: 19 December 2022The Extract Superclass refactoring enables extracting certain members of a class into a superclass. In CLion, this refactoring is available for C++, Objective-C/C++, Python and JavaScript code.
Examples
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
Extract a superclass
Select the desired class in one of the views, or just open it in the editor.
Choose Refactor | Extract/Introduce | Superclass from the main menu or Refactor | Extract Superclass from the context menu.
In the Extract Superclass dialog that appears, specify the following information:
Name of the new superclass in the Extract superclass from field.
Members to be included in the superclass
The Extract superclass from field displays the name of the class, from which a superclass should be extracted. It is read-only.
Click Refactor to proceed with the refactoring. The superclass is created and the original class is modified to inherit from it:
Extracting the class dependent members
Let's consider the following sample of code:
int d1;
class XClass {
static const int d2 = 13;
int exRes = d2 * d1;
};
As you can see, there is a dependency between d1
and exRes
. If you apply Extract Superclass refactoring to the above class and try to select exRes
to be moved, CLion highlights the d2
variable with blue:

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

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.
Thanks for your feedback!