- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Generating Code Snippets without Leaving the Keyboard
- Using Code Completion
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Generating JavaBeans Component Code
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Deleting Code Safely
- Changing a Method's Signature
- Encapsulating a Field
- Moving a Class to a Different Package
- Moving Class Members to Other Classes
- Creating a Method from Existing Statements
- Creating an Interface from Existing Methods
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
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:
- 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.
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.
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.
- 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 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.