Home > Articles > Programming > Java

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

Moving Class Members to Other Classes

The IDE has the Pull Up and Push Down refactoring commands for moving methods and fields to other classes and interfaces. When you use these commands to move class members, the IDE updates references to those members throughout your project. These commands are useful for improving the inheritance structure of your code. You can:

  • Move methods and fields to a superclass or super-interface.
  • Leave method implementations in the current class but create abstract declarations for those methods in a superclass.
  • Move methods and fields to the class's subclasses or sub-interfaces.
  • Move the implementations of methods to subclasses while leaving abstract method declarations in the current class.
  • Move the interface name from the implements clause of a class to the implements clause of another class.

Moving Code to a Superclass

To move a member, a method's declaration, or an implements clause from the current class to a superclass:

  1. In the Source Editor or the Projects window, select the class or interface that contains the member or members that you want to move.
  2. Choose Refactor | Pull Up to open the Pull Up dialog box.
  3. In the Destination Supertype drop-down list, select the superclass or interface that you want to move the members to.
  4. 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.)

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

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

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

Moving Code to Subclasses

To move a member, a method's implementation, or an implements clause from the current class to that class' subclasses:

  1. In the Source Editor or the Projects window, select the class or interface that contains the member or members that you want to move.
  2. Choose Refactor | Push Down to open the Push Down dialog box.
  3. Select the checkbox for each member you want to move.

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

  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 that you would like to reverse, you can choose Refactor | Undo.

  • + Share This
  • 🔖 Save To Your Account