PyCharm 2024.1 Help

Extract superclass

The Extract Superclass refactoring lets you create a superclass for an existing class.

The members of the original class can be moved to the superclass. 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 a superclass and the original class inheriting from the superclass.

Extract a superclass

  1. Place the caret inside a class in the editor.

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

  3. In the Extract Superclass dialog, specify the following:

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

    • Target file where the new superclass will be stored in the Extract superclass with members to directory field.

    • Select the members of the class to be included in the superclass in Extract superclass from.

      If you want the methods in the superclass to be abstract, select the Make Abstract checkbox.

    Extract Superclass dialog

    The Extract superclass from field displays the name of the class, from which a superclass should be extracted. It is read-only.

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

  5. Click Extract to proceed with the refactoring. PyCharm will create a superclass and modify the original class to inherit from it:

    Result of refactoring
Last modified: 11 February 2024