Since we have already discussed about inheritance and abstract classes in JAVA, there are no prizes for guessing that the next frontier of this series of blog posts is interface. Interface is very similar to the abstract classes with more purpose and direction to its functionality. It would not be wrong to say that whatever abstract classes do, interface does better. However; whether to use an abstract class or an interface depends solely on the choice of the programmer and the requirements of the program.

What is an interface?

An interface can be termed as a group of related methods without any specified functionality initialized to them. In other terms, interface is a blueprint of a class with all its methods as abstract methods.

Interfaces in JAVA are aimed at attaining full abstraction and multiple inheritance; something which cannot be achieved by abstract classes and “extends” keyword alone. Let us look at an example of interface in JAVA.

In the above example, we can see that the interface first and second have abstract methods in them and then these interfaces are used by the classes cube and square to perform different functionalities. The keyword for using interfaces in the code is “implements” and the syntax to be followed is, class name followed by the keyword “implements” and then the name of the interfaces in a comma separated order.

Points to Ponder:

All methods under interfaces are/must be public and abstract.

Interface cannot have constructor or an object.

We can extend/inherit interfaces, i.e, one interface can inherit another interface.

Data members of an interface are always public, final and static.

It is necessary to mention access specifier while writing the methods of the interface in a child class.

Once interfaces have been inherited, the methods inside those interfaces cannot be left blank inside the child class. Therefore, the interface data members must implement some functionality inside the child class.

Interfaces are useful in performing multiple inheritance in JAVA since a class can implement more than one interface at a time.

How is an interface different from an abstract class?

It was mentioned at the starting of this blog that there are not many differences between an abstract class and an interface with the latter a better and more useful version of the former. However; understanding of the differences between an abstract class and an interface works wonders for a beginner in realizing the overall concepts of JAVA programming language.

All methods inside an interface are by default abstract, however; it is not necessary that all methods inside an abstract class are abstract.

The data members of an interface are all final while an abstract class may have non-final data members as well.

Members of an interface are by-default public, however; members of an abstract class can be public, private or protected.

The methods inside an interface cannot have an implementation in the interface however; they must have an implementation in the child class. On the other hand, the methods inside an abstract class may or may not have an implementation inside itself or the child classes, however; they need to be mentioned in every inherited class.