Extracting an Interface
See Also
When you use the Extract Interface command, the
IDE creates a new interface from the selected public non-static methods in a class or interface.
Because an interface does not restrict how its methods are implemented, interfaces can be used in classes that have different functions.
Creating interfaces can increase the reusability of your code as you can have multiple classes implementing the same interface.
If necessary, you can then modify the interface instead of making modifications in multiple classes.
When you extract an interface, the IDE does the following things:
- Creates a new interface with the selected methods in the same package as the current class or interface.
- Updates the implements or extends clause of the current class or interface to include the new interface.
Any interfaces that the new interface extends are excluded.
To extract an interface:
- Open the class or interface containing the methods you want to move to an interface.
- In the Source editor, right-click in the file and choose Refactor > Extract Interface.
The Extract Interface dialog box opens.
- Type the name for your interface in the Interface Name text field.
- In the Members to Extract list, select the members that you want to extract to the new interface.
If the class from which you are extracting an interface already implements an interface, there
is also an item for that implemented interface. If you select the checkbox
for that interface, the implements clause for that new interface
is moved to the new interface that you are extracting.
- If you click Refactor, the IDE applies
the changes automatically and skips the remaining steps. If you click Preview,
the Refactoring window displays the lines of code that will be changed. Review
the list and clear the checkbox of any code that you do not want changed. If the
class that you are pushing members from has multiple subclasses and you do not
want the members to be pushed to all of them, be sure to clear the checkboxes
for the corresponding subclasses. Click Do Refactoring to apply the selected changes.
- See Also
- Refactoring: Quick Reference
- Extracting a Superclass
- Extracting a Method
- Renaming a Field or Method
- Undoing Refactoring Changes
Legal Notices