Highlight a class name and select Refactor→ Extract Interface, or right-click the class name and select Refactor→ Extract Interface. Enter the name of the interface you want to extract, select the members to declare in the interface, and click OK.
Eclipse refactoring enables you to extract interfaces from classes.
For example, say you have the code in Example 4-2,
in which the Interfaces
class includes the
nonstatic printem
method.
Example 4-2. A class from which to extract interfaces
package org.cookbook.ch04; public class Interfaces { public static void main(String[] args) { String msg = "No problem."; new Interfaces( ).printem(msg); } public void printem(String msg) { System.out.println(msg); } }
To extract an interface from the Interfaces
class,
right-click that class’s name in your code, and
select Refactor→ Extract Interface, which opens the dialog
shown in Figure 4-6. Select the
printem
method, type in the name
NewInterface
, and click OK.
This creates a new file, NewInterface.java
, with
the new interface you’ve created:
package org.cookbook.ch04; public interface NewInterface { public abstract void printem(String msg); }
You can implement
an
interface by typing the implements
keyword and the name of the interface you want to implement (e.g.,
public
class
ServletExample
implements
NewInterface
). The JDT editor shows a Quick Fix
light bulb to indicate which methods are missing. Click the light
bulb (alternatively, you can press Ctrl-1 or select Edit→
Quick Fix) to let Eclipse implement the missing methods.
Get Eclipse Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.