Home > Articles > Programming > Java

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

Creating an Interface from Existing Methods

If you decide to divide your code into API and implementation layers, you can get started on that conversion by using the Extract Interface command to create an interface from methods in an existing class. The Extract Interface command does the following:

  • Creates a new interface containing declarations for selected public methods.
  • Adds the name of the created interface to the implements clause of the class from which the interface is extracted. (If the interface is extracted from another interface, the name of the newly created interface is added to the extends clause of the other interface.)

To extract an interface from existing methods:

  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 interface.
  2. Choose Refactor | Extract Interface.
  3. In the Extract Interface dialog box, select the checkbox for each method that you want to be declared in the new interface.

    If the class from which you are extracting an interface already implements an interface, a checkbox for that interface is included in the Extract Interface dialog box. If you select the checkbox for that interface, the interface is removed from the implements clause of the previously implementing interface and moved to the extends clause of the new interface.

  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