An interface is a blue print of a class. It defines what a class will do without specifying how to do

You learned about Abstract class in last topic. Abstract class can have abstract methods and normal concrete methods. but what if we want define a class with all abstract methods only ? For this we use interfaces. Think of a interface as a complete abstract class.

Implementation

An interface can be declared using keyword interface. Below is the syntax of using it

interface interface-name {
	// constants declaration
		...
	// methods declaration
		...

}
Declaring an interface is similar to declaring a class. You just have to use interface instead of class. Lets define one interface .

 

interface Car{
	
	void accelerate();
	void brake();
}

In above example we have defined an interface Car that has two methods. But we have not declared those methods as abstract, why ? As we said interfaces are supposed to have all abstract methods only, Java has made it default for interfaces. It means, you don't have to declare a method in interfaces as abstract. So above interface is same like shown below.

	interface Base{

	abstract void accelerate();
	abstract void brake();
}

If you remember a rule from abstract class, a class has to declare itself as abstract if it contains any abstract method but our interface contains all abstract methods only then why didn't we specify abstract in interface definition ? that's because since interface is going to hold only abstract methods, Java has made it easy for you by making interfaces as abstract implicitly. So our interface in reality looks like this

abstract interface Base{
	
}

Since interface has all methods as abstract, we have to have at least one child class that implements all methods of interface as like in case of abstract class. And how do we do that ? To make a class a child class of interface we use keyword implements instead of extends. See below for a class CarModelOne implementing interface Car.


class CarModelOne implements Car {


 public void acceleate(){	// Notice public here
 // Implementation here

}

public void brake(){	// public here too
// Implementation here

}

}

One more difference from abstract classes is that a class can implements any number of interfaces. You just have to put one semicolon between all interfaces name.

class Classname implements InterfaceOne, InterfaceTwo{
	
// Implements methods from both interfaces.

}
Since our class implements two interfaces, it has to implement methods from both interfaces. Like showed above, a class can implement any number of interfaces.

Methods in Interfaces

Did you notice we have declared both two functions as public in our class CarModelOne? It is mandatory for all child classes to declare all methods it implements from any interface as public. Why ? because interface defines all methods as public by default and you can not reduce the scope of visibility of any method while implementing them. You will be reading about this in our next topic Polymophism. For now just remember this as a rule.

Why interface's methods are public anyway ?

As said in introduction of interfaces, it is a blueprint and defines what a class will do. Any class can implement an interface and since our class can be present in any package, it is necessary for interface to declare there methods as public to made them visible for all classes irrespective of the package they belong to. For more detail read Access Modifiers. It means interface has to declare itself also as public. So our interface Car has to be declared as shown below.

public interface Car{
	
}
But it is not mandatory. Since all methods in interfaces are public by default, declaring interface as public explicitly doesn't makes sense. So Java has made interface as public by default.

Child class has to declare all methods of an interface as public while implementing.

Knowing interface is public and abstract by default, following are some valid declaration of interfaces. If you do not specify any of the keyword, Java compiler will put it there while compiling.

public abstract interface Car{}
public interface Car{}
abstract interface Car{}
interface Car{}

Similarly as methods in interface are abstract and public by default, Following are valid methods declaration in an interface.

void show();
abstract void show();
public abstract void show();
public void show();
You can not change access specifier of methods in an interface. You can not use protected, private and default access modifiers for methods. If you do try, you will get compilation error.

Variables in interfaces

So far we talked about methods in interface, what about variables ?

In interface, all the variables shall be public ,static and final. It means an interface can define only constants. Since it is mandatory for interface to have constants only, you do not have to declare them explicitly, they will be by default.

interface Car{
	
	maxGearNumber = 4;
	// it is same like one below.
	public static final maxGearNumber = 4;
}

Multiple inheritance

We had said Java does not support multiple inheritance. But it does support it partially in case of interfaces. An interface can extend any number of interfaces. Below is an example showing how to use it.

				// You can extends any number of interfaces.
	interface Base extends InterfaceOne, InterfaceTwo{
		..
	}
	
In above example, interface Base will inherit all methods and variable from interfaces InterfaceOne and InerfaceTwo. Any class that implements interface Base will now have to implement all methods declared in interface Base as well as inherited by interface Base from other two interfaces.

Combining extends and implements

Any class can extends another class as well as implements any interface.Remember a class can extend only one class at a time but can implement more then one interfaces. Below is an example showing syntax and order of using extends and implements together.

class Child extends Base implements MyInterface1, Myinterdace2 {

}

Here, the order of extending a class and implementing the interfaces is important. A class must extends a class first followed by the implements.

WHAT YOU SHOULD KNOW NOW

  • interface is the keyword to define an interface.
  • All methods are by default public and abstract in an interface.
  • All variables are by default public, staticand final in an interface.
  • implements is the keyword used to implement an interface by any class.
  • Child class can implement any number of interfaces at once.
  • Child class has to declare all inherited methods from interface as public.
  • An interface can extends any number of interface

Please post your questions at Ask