View RSS Feed

My Java Tips

Extract Interface

Rate this Entry
by , 11-20-2011 at 05:43 PM (1285 Views)
Eclipse provides the possibility to extract interface from a class. You may call it reverse engineering since rule of thumb is to write the interface first and then write the classes implementing it. But sometimes, itís the other way around. You write a class and then you want to generate the appropriate interface for it. Lets see how to do this.


I wrote a class that has to do with text extraction from CSV files. I named it CSVFile.

Java Code:
import java.util.ArrayList;

public class CSVFile {
	public static void main(String[] args) {

	}

	public ArrayList extractHeader()
	{
		return null;
	}
	public int getNoOfRows()
	{
		return 0;
	}
	public ArrayList extractContents()
	{
		return null;
	}

}
Then I realized that I should have an interface for it as well. ofcourse, I can simply write it, but I am looking for an easy way. Thankfully I am using Eclipse and it supports this thing.

In order to generate the interface for CSVFile class, right click it from the package Explorer > Re factor > Extract Interface.




You may choose the method whose signature you want in the interface. In my example, I need all in the interface, so I checked all.

The interface was generated and implemented also.

Java Code:
import java.util.ArrayList;

public interface IFile {

	public abstract ArrayList extractHeader();

	public abstract int getNoOfRows();

	public abstract ArrayList extractContents();

}

import java.util.ArrayList;

public class CSVFile implements IFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see IFile#extractHeader()
	 */
	public ArrayList extractHeader()
	{
		return null;
	}

	/* (non-Javadoc)
	 * @see IFile#getNoOfRows()
	 */
	public int getNoOfRows()
	{
		return 0;
	}

	/* (non-Javadoc)
	 * @see IFile#extractContents()
	 */
	public ArrayList extractContents()
	{
		return null;
	}

}

Submit "Extract Interface" to Facebook Submit "Extract Interface" to Digg Submit "Extract Interface" to del.icio.us Submit "Extract Interface" to StumbleUpon Submit "Extract Interface" to Google

Comments