CLion 2023.2 Help

Extract superclass

The 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

class SomeClass { char SomeChar = 'a'; long d22; //This function will be extracted to a base class void exFunc(int); };
class SuperClass { void exFunc(int); }; class SomeClass : public SuperClass { char SomeChar = 'a'; long d22; };

Before

After

@interface SClass : NSObject { int v; //This variable will be extracted to a superclass } - (void)initV; @end @implementation SClass - (void)initV { v = 20; } @end
@interface SuperClass : NSObject { int v; } @end @interface SClass : SuperClass - (void)initV; @end @implementation SClass - (void)initV { v = 20; } @end

Before

After

class BaseClass: # This function will be extracted to a base class def eval_smth(self, a, b, c): x = 2*b - c y = 2*b + c return x, y
class SuperClass: def eval_smth(self, a, b, c): x = 2*b - c y = 2*b + c return x, y class BaseClass(SuperClass): pass

Before

After

class Editor { //This function will be extracted to a superclass View() { console.log(this.name + ' can view'); } Edit() { console.log(this.name + ' can edit'); } }
class User { View() { console.log(this.name + ' can view'); } } class Editor extends User { Edit() { console.log(this.name + ' can edit'); } }

Extract a superclass

  1. In the editor, select the class members that you want to extract into a superclass.

  2. Choose Refactor | Extract/Introduce | Superclass from the main menu or Refactor | Extract Superclass from the context menu.

    Extract superclass in the context menu
  3. In the Extract Superclass dialog, specify the following:

    • Name of the new superclass in the Extract superclass from field.

    • Members to be included in the superclass.

    Extract Superclass dialog

  4. Click Preview to check the refactoring result before proceeding. CLion will notify you in case of problems.

    Problems detected during Extract Superclass

    Dependency problems are also highlighted in the Extract dialog:

    Dependency problems in Extract Superclass
  5. Click Extract to proceed with the refactoring. CLion will create a superclass and modify the original class to inherit from it.

Last modified: 15 September 2023