Home » Java » Abstract methods & Interfaces » Abstract methods & Interfaces

Abstract methods & Interfaces

Abstract methods & Interfaces

Interfaces

When dealing with an abstract class, we sometimes see what’s called an interface. An interface is a definition of all abstract methods however, unlike abstract methods within an abstract class, these methods defined in the interface can be used in any other class, regardless of being abstract or not.

Here is how to define an interface:

interface Name {
       //methods here
}

The methods that are defined in the interface DO NOT need the abstract keyword with them.

In order for a class to use an interface, it must use the keyword implements in it’s class header. Here is the definition:

public class Name implements interfaceName {
	//code here
}

where Name is the name of the class and interfaceName is the name of the interface you will be implementing.

By default, all methods in an interface contain the keywords abstract public before each definition so these are not needed. You also cannot make the methods private or protected as this is not allowed in Java interfaces.

Let’s look back at the previous chapter’s example of the Food stores. Here is the full example using an interface:

Example 1:
Simple Interface example

public interface Slogan {
	String slogan();
}

public abstract class FoodStore implements Slogan{
	private String name;
	private int opened;

	public FoodStore(String n, int o){
		if(n == null)
	 throw new IllegalArgumentException("No name given!");
		else if(o < 0 || o > 2010)
	 throw new IllegalArgumentException("Bad opening!");
		else{
		   name = n;
		   opened = o;
		}
	} //constructor

	public String toString(){
	  return("Name: " + name + "nOpened: " + opened);
	}
} //class

public class BestFoods extends FoodStore {
	private String slogan = "The best food anywhere!";

	public BestFoods(String n, int o){
		super(n,o);
	}

  //implements the slogan() method from the interface:
	public String slogan(){
		return slogan;
	}
} //class

public class OldFoods extends FoodStore{
   private String slogan = "The oldest food store around!";

    public OldFoods(String n, int o){
		super(n,o);
	}

        //implements the slogan() method:
	public String slogan(){
		return slogan;
	}
}

public class WholesomeFoods extends FoodStore {
	private String slogan = "Mmmmmm, delicious!";

	public WholesomeFoods(String n, int o){
		super(n,o);
	}

        //implements the slogan() method:
	public String slogan(){
		return slogan;
	}
}

public class Stores{
	public static void main(String args[]){
		FoodStore fs[] = new FoodStore[3];

	fs[0] = new BestFoods("Best", 1956);
	fs[1] = new OldFoods("Old", 1944);
	fs[2] = new WholesomeFoods("Whole", 1987);

	for(int i = 0; i < 3; i++)
        System.out.println(fs[i] + "nSlogan: "
         + fs[i].slogan() + "n");

	} //main
} //class

Since each subclass is inheriting all methods from the super class FoodStore, it is not necessary to place the implements Slogan in each subclass. If you did that, you would get an error citing that the method from the interface is undefined and the super class must implement the interface.

The output is also the same as the last chapter. All we did was change the abstract method in the class to an interface.

Interface constants

Constants can be placed in an interface. This is perfectly legal in Java.

Here is an interface example that uses a constant:

public interface Calendar {
	int MONTHS = 12;
	int DAYS = 365;
}

public class Year implements Calendar{
	public static void main(String args[]){
		int m = 12;
		int d = 366;
	if(d == DAYS)
            System.out.println("Equal days!");
	if(m == MONTHS)
            System.out.println("Equal months!");

        } //main
} //class

The output would simply be “Equal months!”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: