Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

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, whether now or in the future.

You can use the Extract Superclass command 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 command 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:

  1. 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.
  2. Choose Refactor | Extract Superclass.
  3. In the Extract Superclass dialog box, 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 dialog box. 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.

  4. Click Next.

    If you have deselected the Preview All Changes checkbox, the changes are applied immediately.

    If you leave the Preview All Changes checkbox selected, the Refactoring window appears with a preview of the changes.

  5. 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.
  6. Click Do Refactoring.

    If you later find that the refactoring has had some consequences you would like to reverse, you can choose Refactor | Undo.

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 command to move methods from other classes to the new superclass. As with the Extract Superclass command, 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.
  • For other classes that you want to extend the new superclass, use the Override and Implement Methods feature (Ctrl-I) to add methods from the superclass to those classes. See Generating Methods to Override from Extended Classes earlier in this chapter.
  • Use the Use Supertype Where Possible command to change references in your code to the original class to the just created superclass. See Changing References to Use a Supertype below.
  • + Share This
  • 🔖 Save To Your Account