Introduction to Java Interfaces




Image Credits - Pexels

An interface is an outline or description of the operations that an object is able to perform. It doesn’t include any implementation part. For a real world analogy, consider a television remote controller. You don’t need to know about the internal circuitry of the remote controller in order to use it. All you need to know is what the buttons do. Likewise, in Java interfaces, you just provide an overview of WHAT an object can do. It doesn’t give info on HOW the object does that.

Abstract Methods Revision

I have introduced you the abstract methods in the lesson Polymorphism. Quick revision – abstract method is a method where you don’t implement the code, but just give its signature. An abstract method must be inside an abstract class. That class cannot create its own object, but can become a parent class to a class which extends it.

Eg:

Here, the Class Tree is an abstract one. It has an abstract draw() method. See, you don’t write code for the draw method. It must be written in all Classes which becomes a child Class of Tree.

Eg:

Now two questions might arise.

  1. Can all methods in the abstract Class Tree be abstract?
  2. Can OrangeTree Class have more than one parent Class?

And the answers for the questions:

  1. Yes
  2. No!

Let’s focus on this 2nd question.

Multiple Inheritance

Having more than one parent Class for a Class is called Multiple Inheritance.

Multiple Inheritance is not supported in Java

This multiple inheritance is something that is NOT allowed directly in java! Seems to be pretty unfair though 🙂 Because other programming languages like C++ allow this. But in Java, no chance mate…

The reason for this is the increase of complexity in multiple inheritance. Java always try not to confuse the programmer and therefore make the language easy to understand and clear. Multiple inheritance may have bad outcomes than the good it does 🙁 So, it is avoided.

Back to our topic. Interfaces are related to both of the previous questions. Let’s see what’s an interface.

So, what is an Interface?

It is a feature of Java where all the methods in it are abstract methods*. Just like a usual abstract Class which has only abstract methods, but there are two big differences.

  1. Abstract classes can contain non-abstract methods that have their own implementation code in it. But all the methods in an interface MUST be abstract and no implementation for a method is allowed.
  1. A child Class can have only ONE parent Class. Even all the members of the parent Class are abstract, you can have one and only one parent Class. But you can have ANY number of “parent” interfaces in a child Class.

That means, though multiple inheritance is not directly allowed, you have a detour to do that! Use interfaces.

Doesn’t this increase the complexity of the code? Yes it does. But this time, it is not confusing!

Since all the members of interfaces are abstract, we have to implement all of them one by one in our working Child class.

This means, we should and we will have a good understanding about the meaning of this interfaces by the time we implement it. If we use Classes instead, we might not know the full details of all the Classes we inherit.

One important thing! If you use an interface in your program, you have to use the keyword implements, not extends.

Eg:

Here, the class X is a child Class of the Class A and it implements the 3 interfaces B, C and D. See, the 3 interfaces can be written by putting commas, in the Class definition.

As another example, let’s make a Tree interface.

Note that you have to initialize the two member variables height and branchesNo.

Now an OrangeTree Class that implement Tree interface;

An interface can be implemented using an anonymous inner class as well.

eg:

*Default Methods

I have mentioned that all methods in Java interfaces are abstract. But,  In Java 8, which is the current latest version,  you can define methods that are not abstract! They are called as “Default Methods” or “Defender Methods“.

Here, you can see that there’s a special keyword default in the method defaultMethod(). This allows adding code to your method. Now the interesting fact is that when you implement this NewInterface, you are not compulsory to write an implementation for the defaultMethod(). If you don’t override it, the default implementation is executed.

Just think that you are updating from previous Java version to Java 8. Now you redesign an interface written from that previous version of Java and add an implementation to a method in it using this technique.

There could be hundreds, thousands of Classes that implements your previous interface. But note that, your modification won’t affect any of them. Since each of them has overridden all the methods in the interface,  they have unique implementations of the methods. This is therefore useful in future coding that you will do by implementing that modified interface.