Extracting a Superclass to Consolidate Common Methods

Extracting a Superclass to Consolidate Common Methods

As a project evolves, you might need to add levels to your inheritance hierarchy. For example, if you have two or more classes with essentially duplicate methods that are not formally related, you might want to create a superclass to hold these common methods. Doing so will make your code easier to read, modify, and extend, now and in the future.

You can use the Extract Superclass feature to create such a superclass based on methods in one of the classes that you want to turn into a subclass. For each method that you add to the superclass, the Extract Superclass feature enables you to choose between the following two options:

• Moving the whole method to the superclass •

Creating an abstract declaration for the method in the superclass and leaving the implementation in the original class

To extract a new superclass, in the Source Editor or the Projects window, select the class that contains the methods that you want to be extracted into the new superclass. Choose Refactor ➤ Extract Superclass. The Extract Superclass window opens, as shown in Figure 6-19 .

Figure 6-19. Extract Superclass window

In the Extract Superclass window, select the checkbox for each method and field that you want to be moved to the new superclass. Private methods and private fields are not included. If you want to leave

a method implementation in the current class and create an abstract declaration for the method in the superclass, select the Make Abstract checkbox for the method.

If the class from which you are extracting a superclass implements an interface, a checkbox for that interface is included in the Extract Superclass window. If you select the checkbox for that interface, the interface is removed from the implements clause of the class that you are extracting from and moved to the implements clause of the new superclass.

If you click Refactor, the changes are applied immediately. If you click Preview, the Refactoring window appears with a preview of the changes, as shown in Figure 6-20 .

Figure 6-20. Refactoring window

In the Refactoring window, look at the preview of the code to be changed. If there is a modification that you do not want to be made, deselect the checkbox next to the line for that change. Click Do Refactoring. If you later find that the refactoring activity has made some changes that you want to reverse, you can

choose Edit ➤ Undo (Ctrl+Z).

After you have extracted the superclass, you can use a combination of the following techniques to complete the code reorganization:

Add the name of the new superclass to the extends clause of any other classes that you want to extend the new superclass.

Use the Pull Up feature to move methods from other classes to the new superclass. As with the Extract Superclass feature, you can move whole methods or merely create abstract declarations for the methods in the new superclass. See “Moving Class Members to Other Classes” earlier in this chapter.

Use the Use Supertype Where Possible feature to change references in your code to the original class to the just created superclass. See the section “Changing References to Use a Supertype” that follows.