Moving Code to a Superclass

Moving Code to a Superclass

To move a member, a method’s declaration, or a current class to a superclass, in the Source Editor or the Projects window, select the class or interface that contains the member or members that you want to move. Choose Refactor ➤ Pull Up to open the Pull Up window, as shown in Figure 6-12 .

Figure 6-12. Pull Up window

In the Destination Supertype drop-down, select the superclass or interface that you want to move the members to. Select the checkbox for each member that you want to move. If you want to leave the 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 moving members implements an interface, a checkbox for that interface is included in the dialog box. If you select the checkbox for that interface, the interface is removed from the implements clause of the current class and moved to the implements clause of the class to which you are moving members. If you select a checkbox for an interface, be sure that all the checkboxes for the methods declared in that interface are also selected.

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-13 .

Figure 6-13. 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 had some effects that you want to reverse, you can choose

Edit ➤ Undo (Ctrl+Z). Moving Code to Subclasses

To move a member, a method’s implementation, or the current class to that class’s subclasses, in the Source Editor or the Projects window, select the class or interface that contains the members that you want to move. Choose Refactor ➤ Push Down to open the Push Down window, as shown in Figure 6-14 .

Figure 6-14. Push Down window Select the checkbox for each member you want to move to the subclasses.

If you want to leave an abstract declaration for the method in the current class and move the implementation to the subclasses, select the Keep Abstract checkbox for the method. If the class from which you are moving members implements an interface, a checkbox for that interface is included in the window. If you select the checkbox for that interface, the interface is removed from the implements clause of the current class and moved to the implements clause of the class to which you are moving members.

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-15 .

Figure 6-15. 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 had some consequences that you would like to reverse,

you can choose Edit ➤ Undo (Ctrl+Z).