What is the actual Use of interface in Java? (original) (raw)

An interface in Java has remained a complex topic for many beginners to understand. The first thing which puzzles many programmers is the fact that you cannot define any method inside interface, it a just declaration. By rule, all method inside interface must be abstract (Well, this rule is changing in Java 8 to allow lambda expressions, now interface can have one non-abstract method, also known as a default method). So, if you can't define anything, Why we need an interface? what's the use of an interface, if we are anyway going to write a class and override them to provide behaviour, Can't we declare those methods inside the class itself without using interface etc. Well, if you are thinking in terms of behaviour then you are really missing the point of interface.

I think one has to read Effective Java, to understand best use of interface. Interface is great to declare Type, they promote code reusability, and they are the real driver of polymorphism in Java.

The interface also allows multiple inheritance in Java, which makes it possible for a class to become Canvas, as well as EventListener, which is used to draw graphics as well as to to process events.

In this post, I will share few points, which will help you to understand what is the actual use of interface in Java. By the way, if you are confused between abstract class and interface, then you may want to read my previous post on difference between interface and abstract class in Java.

Why we need Interface in Java?

There are several reasons, an application developer needs an interface, one of them is Java's feature to provide multiple inheritance at interface level. It allows you to write flexible code, which can adapt to handle future requirements. Some of the concrete reasons, why you need interface is :

  1. If you only implement methods in subclasses, the callers will not be able to call them via the interface (not common point where they are defined).

  2. Java 8 will introduce default implementation of methods inside the interface, but that should be used as exception rather than rule. Even Java designer used in that way, it was introduced to maintain backward compatibility along with supporting lambda expression. All evolution of Stream API was possible due to this change.

  3. Interfaces are a way to declare a contract for implementing classes to fulfil; it's the primary tool to create abstraction and decoupled designs between consumers and producers.

What is the Actual Use of interface in Java?

  1. Because of multiple inheritances, interface allows you to treat one thing differently. For example a class can be treated as Canvas during drawing and EventListener during event processing. Without an interface, it's not possible for a class to behave like two different entity at two different situations.

Here is an example of how interface supports multiple inheritance in Java

interface Canvas{ public void paint(Graphics g); }

interface EventListener{ public boolean process(Event e); }

pubic class Game implements Canvas, EventListener{

@Override public void paint(Graphics g){ g.drawLine(Color.RED); }

@Override public boolean process(Event e){ KeyCode code = e.getKeyPressed().getCode(); }

}

  1. Interface is key to API design. In fact, a smaller interface like Comparable, Runnable, Callable makes the core of Java API. Though great care is required while designing and publishing an interface, because once published, you can not change the interface without breaking up all your clients, i.e. classes which have implemented your interface.

In an extreme case, from Java 8 onwards, you can use the default method to rescue, but as I said, it should be the exception to the rule.

Why we need interface in Java

  1. "Programming to interface than implementation" is one of the popular Object-oriented design principles, and the use of interface promotes this. A code written on the interface is much more flexible than the one which is written on implementation.

  2. The use of interface allows you to supply a new implementation, which could be more robust, more performance in the later stages of your development.

In short main use of the interface is to facilitate polymorphism. the interface allows a class to behave like multiple types, which is not possible without multiple inheritances of class. It also ensures that you follow programming to the interface than the implementation pattern, which eventually adds a lot of flexibility in your system.

Other Java programming and OOP tutorials You may like

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.