Java Interface

A Java interface (not to be confused with graphical user interface) is a list of methods without executable code, and only contains the name, parameters, and return type of these methods.

Analogy

To understand interfaces, it is useful to consider a real world analogy. A power socket, for example, is an interface for electric appliances because it conform to a list of specifications. In the U.S. an outlet must provide voltage around 120 V, and frequency of 60 Hz. Any appliance that is designed for this specification will work with this socket.

Similarly, Java interfaces, often called contracts, specify what implementing classes must do. For example, the Bag listed below is a Java interface.

Any class that implements this Bag interface must provide the code for all the methods (specs) listed. For example, the ArrayBag class below implements the Bag interface:

Notice the ArrayBag class must have all the methods listed in the Bag interface and their code. Other classes that uses our ArrayBag class knows how to to use it because ArrayBag follows the spec in Bag. In terms our analogy:

  1. The interface <=> the specs of the socket
  2. The class that implements the interface <=> the actual socket
  3. And other classes that use our class <=> appliances

In a sense, a Java interface is a blueprint for classes.

Failure to Comply

What if a class implements an interface but does not have all the methods listed in the interface? In this case the class does not conform to the spec. In real world, if a socket does not follow the spec, it could damage our gadget. In Java, the compiler will catch this error and your program will not compile.

Putting It Together

Because interface specify what methods an implementing class must have, we often see interfaces being used as parameter and return types, as shown below.

Note: Comparator is an interface.

Obviously we cannot create an instance of an interface types, so what do we pass into the sort() method for parameter C, which has an interface type? We can pass in an instance of any class that implements the Comparator interface, as shown below:

Last, why would we even want to use interface parameters? Why don’t we just always use class types as parameters? Well, since an interface defines what methods are included in a class, that is all we really need to interact with a class, and we don’t need to know how those methods are implemented.

The beauty of this is that we can have two classes that implement an interface in two completely different ways, but as long as these classes implement the methods, they can passed to an interface parameter. Car engines don’t have to be designed the same way, but as long as they follow certain specs, they are reusable components.

One thought on “Java Interface”

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + 9 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">