In this article we will see, what is an interface in Java, how to declare an interface and methods and variables declaration in interfaces.

Interfaces are kind of a contract in Java Programming, what contract means here is you can have a number of method declared in an interface and every class that is implementing this interface must implement all methods of the interface, for example you can have a interface Car like:


public interface Car {
	public abstract void speed();
}

In the code snippet above we have declared an interface Car with an abstract method speed(), every class that is implementing Car must have to implement speed() methods as per need.

class Swift implements Car {

	@Override
	public void speed() {
		// speed implementation for Swift
	}

}

class Alto implements Car {

	@Override
	public void speed() {
		// speed implementation for Alto
	}

}

Class Swift and Alto are implementing Car interface, so they need to implement speed(), they can implement it any way but they have to implement the speed() method that is a contract.

Interfaces can have abstract, public methods only, all the methods of an interface are public abstract no matters if you put public, abstract or both modifiers with them or not. See the example of some valid interface declarations:

public abstract interface Human{
	public abstract void speed();
}
interface Human{
	public abstract void speed();
}


interface Human{
	public void speed();
}
interface Human{
	abstract void speed();
}
interface Human{
	int speed(Boolean b);
}

So in interfaces we can have any public, abstract or having both modifiers, but all the methods will be treated as public abstract methods by default. Additionally we can not have private or protected modifiers with methods and not even final or static.

In interfaces we can have final variables only, no matter if we declare them as final or not. All the variables in Interfaces are treated as constants and hence need to be initialised at the time of their declaration.

interface Human{
	int a=1;
}

interface Human{
	final int a=1;
} 

An interface can extend other and any number of interfaces but can not extend an interface because of having no room for implementation. See the example:

public interface Car {
	public abstract void getSpeed();
}

interface Plants {
	public void getColor();
}

interface Human extends Car, Plants {
	public void getName();
}

If an interface extends any other interface or a number of interfaces than the class that is implementing the interface has to implant all the methods of interfaces in the hierarch. See the example:

interface Human extends Car, Plants {
	public void getName();
}


class MyClass implements Human{

	@Override
	public void getSpeed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getColor() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getName() {
		// TODO Auto-generated method stub
		
	}
	
}

As the example show, MyClass implements Human and hence needs to implement all the methods of other interfaces that are being extended by Human.

In this particular article we saw what are interfaces in Java, how to define them and declaration or variables and methods in interfaces. In coming articles we will see more about Java and other open source technologies.
  • By Techburps.com
  • Mar 24, 2015
  • Core Java