Extract Superclass refactoring
Refactor | Extract/Introduce | Extract Superclass...
This refactoring allows you to extract certain members from a selected class into a new base class. The original class will extend the created base class.
If the current type already implements any interfaces, those interfaces can also be extracted into the new base class.
If you want to create an abstract class, mark at least one of the extracted method abstract.
In the example below, we extract an abstract class Shape
from the existing class Circle
:
Before refactoring
public class Circle : IDrawable{ public Point Center { get; private set; } public Color MyColor { get; private set; } public int Radius { get; private set; } public void Draw() { // draw... }}public interface IDrawable{ void Draw();}
After refactoring
public abstract class Shape : IDrawable{ public Color MyColor { get; private set; } public abstract void Draw();}public class Circle : Shape{ public Point Center { get; private set; } public int Radius { get; private set; } public override void Draw() { // draw... }}public interface IDrawable{ void Draw();}
tip
You can also extract an interface from the current class using the Extract Interface refactoring.
Select a class in one of the following ways:
In the editor, place the caret at the name of a class.
Select a class in the Solution Explorer.
Select a class in the Structure window window.
Do one of the following:
Press CtrlShift0R and then choose Extract Superclass.
Choose Refactor | Extract Superclass from the main menu.
The Extract Superclass dialog will open.
Specify a name for the new base class and where it should be placed — in a new file or in the same file as the original type.
Select members that you want to transfer to the new base class. The list of members can contain:
Members of the current type
Interfaces that the selected type currently implements
To make/keep individual members abstract, tick the checkbox in the Make Abstract column next to them. If there is at least one member marked abstract, the base class will be created abstract too.
To quickly select all public members, click Select Public
Alt+P.
If you select members that reference other members, there will be undefined symbols in the extracted class. Click Dependent to resolve the conflict by selecting all members that depend on the currently selected members.
To apply the refactoring, click Next.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action Ctrl0Z to roll back all these changes with a single keystroke.
