PyCharm 2022.2 Help

Extract superclass

The Extract Superclass refactoring lets you create a superclass for an existing class . In this case, PyCharm changes all original class usages to use a superclass where possible.

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 that appears, specify the following information:

    • 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 Refactor to proceed with the refactoring. The superclass is created and the original class is modified to inherit from it:

    Result of refactoring
Last modified: 28 September 2022