Interfaces in Java

Interfaces

In Java, interfaces are the set of requirements for the class that it has must to implement. Interface is not a class instead it is the abstract data type we can say that we have to implement in class. Likewise, we have a interface for a Stack where we have push() method and pop() method and while implementing stack interface we have to define push() and pop() methods in our class. Pay attention to code below here:

Similarly, we have an interface for other data types also like queue and in particular java has an interface Comparable. To understand the concept of interface in java, let's consider another interface java Shape and we will look on classes later on. 

Interface in Java

Now let's look on Shape interface that has area() method to return area of the Shape which may be Rectangle, Triangle, Circle etc.

Why to use Interfaces?

The reason why we use interfaces in java that java does not allow multiple inheritance but it allows multiple interfaces to be implemented as same class have properties from many other classes. Interface ensures that those properties are implemented properly. If Java developers allow multiple inheritance in Java like in C++, things gone to be very complex and they decide to use interfaces in Java instead of multiple inheritance.

Interfaces in Java

 There are pre-built interfaces in Java written by other programmers that we will discuss here:

  • Cloneable Interface
  • Comparable Interface
  • List

So, here we will focus on Comparable interface that contains a method compareTo() to be impemented by classes that implements it. The code for Comparable interface is:

Now after looking at above code, you may have an idea of interfaces in Java. Let's discuss the method implemented here in detail which is compareTo(). It returns a integer value which may three cases as:

  • If both objects are equal, it should return 0
  • If val1 > val2, it should return 1
  • If val1 < val2, it should return -1

Interface documentation recommends that everyone implementing this interface method, must follow this instructions but compiler will not check these conditions even if you return 0 without any method body and that will work fine. These instructions are documented as Java Docs associated with each method of interface.  

Note that we never define a method in interface. We only write a method signature in interface and a concrete class that implements that interface must define that method.

Interfaces and Inheritance in Java

Interface Methods

Interfaces in java are always implemented by a concrete class. There are many questions arise here. What if abstract class implements another interface or a interface extends another interface? Why interface is extending another interface not implementing it? So, before answering these questions we first understand the concept in depth. Interfaces in java have method or more than one methods and we do not define them instead we only write method signatures. 

 For Getting My Services

Java Programming Projects

Methods in java are always public by default and we do not need to write public with them because it will be added implicitly. On the other hand, interface may also contain some constant variables, not instance variables, which are always public static and final by interfaces documentation and here we also do not need to write public final static with them.  

Can we create a object of interface?

No, we cannot create a object of interface like abstract class but we can create a reference variable of interface pointing to object of class that implements it.

Implementing a Interface

A concrete class can implement a interface by implements keyword. When a class implements a interface, it writes public class ABC implements XYZ. XYZ is interface name. We have made a Shape interface above and now we will see how that interface is being implemented by different classes.

Triangle class is implementing a Shape interface and it defines its own method for area() but with different method body. Let's consider on Rectangle class implementing a Shape interface.

Now, you have seen each class can implement a interface methods according to its own needs. Note that an interface can also contain a general <T> template type that we can pass while implementing and it can prevent us from typecasting. In latest versions of java, interface Comparable is written as:

So, you have seen interfaces and gets an idea of its implementation. Let's explore our questions that we have discussed before and study conflicting interfaces.

 What if abstract class implements an Interface?

If abstract class implements a interface, it must define interface methods to be abstract or define it. If it define it as a abstract, that method will be defined by any concrete class in its inheritance hierarchy.


Conflicting Interfaces

In conflicting interfaces, we may have an conflict of one method of same signature defined in same inheritance hierarchy of interfaces and what about constant values that will not change etc. Consider this code to understand all concepts clearly.

After reviewing above code, you can understand the concept of interfaces and its features including static, default methods etc. But consider another interface that extends this interface and contain same methods as in in this interface. All conditions are written in code in comments.

Java allows static, default methods in interfaces and also we can give give overloaded methods.

Why Interfaces instead of Abstract Classes?

So, why we use interfaces instead of abstract classes are that a concrete class can inherit only single abstract class at a time but a class can implement a multiple interfaces at a time. Abstract classes may have instance variables but they must contain at least one abstract method. On the other hand, interfaces only have methods that have to be implemented. In addition, they may contain static, default methods or final variables which are static and public. This will not affect interface property to be implemented many in a single class.


Post a Comment

Previous Post Next Post