View RSS Feed

My Java Tips

Generating Interface from a Class

Rate this Entry
by , 11-14-2011 at 06:27 PM (1216 Views)
Some times you have a class with concrete methods and you want to declare an interface with all the methods in the class. You may call it backtracking. In this post, I will write about how to do this in Eclipse.

Suppose we have a class called Students that has methods defined in it. We want to generate the interface for it in Eclipse. Simply right click the class name, or simply right click anywhere in the code and select Refactor > Extract interface. A new window will open. Name the interface and select the methods you want to put into the interface. All the methods in the Students class will be listed in the window and by default none will be selected. After naming and selecting the methods, you can get the preview as well or simply click ok to generate the interface.

The signature of Student class will also change to:

Java Code:
public class Students implements StInterface  {

This approach is a backtracking approach and generates the interface in no time. Try it.

Submit "Generating Interface from a Class" to Facebook Submit "Generating Interface from a Class" to Digg Submit "Generating Interface from a Class" to Submit "Generating Interface from a Class" to StumbleUpon Submit "Generating Interface from a Class" to Google