Interface in Java
In the previous blog we learned about the Reflection in Java - Part 2. If you want to learn the concept of Reflection then visit Reflection in Java Part 2 . In this blog, we will go through the concept of Interface. The interface is a mechanism used for describing what a class should do without describing how they should do it. In other words, Interface is a mechanism used to achieve abstraction. We will go through the subtopics of interfaces one by one:
Interface
Interface is a mechanism to achieve abstraction. The interface can have only abstract methods and not the method body. The interface is used to achieve abstraction and multiple inheritance in Java. In other words, we can say that it can only have abstract methods and variables. Java interface represents the IS-A relationship. We will go through the example. The sort() method of an Array class assures to sort the array on an object but under one condition i.e. the object should belong to the class that implements Comparable interface.
e.g.
In the above code we can see the Comparable interface which has compareTo() in its definition. Whenever any class implements a Comparable interface, then it is mandatory to have compareTo() and the compareTo() method should take an Object parameter and must return an integer. All the methods that are defined in the interface are public by default. So it is not mandatory to write a public keyword while declaring the method in an interface. While using the compareTo() method there is a requirement that when obj1.compareTo(obj2) will be called, then the compareTo() must be able to compare two objects and must return the result whether obj1 or obj2 is larger. The method must return the result as a negative number if obj1 is smaller than obj2, 0(zero) if both are equal, or return positive no. The interface defined above has only one method, but it is possible that the other interfaces may have multiple methods. We can also define constants in the interface. We cannot define instance fields in an interface. We can assume that an interface is an abstract class with no instance fields. We will take an example, suppose we want to use sort() of Array class to sort an Array of AtrowelEmployee object. In order for AtrowelEmployee class to implement an interface, you have to perform 2 steps:
- Class must intend to implement the interface.
- Provide definitions for all the methods in the interface.
In order to declare that the class implements an interface, we must use the implements keyword.
e.g.
Supply the compareTo() method to the AtrowelEmployee class. Suppose we want to compare the salaries of an AtrowelEmployee then we can use compareTo() method.
e.g.
In the above code the return statement will return negative if the first argument is less than the second argument, 0 if both are equal or will return positive. There might be a question that why any class cannot use the compareTo() method without implementing it in the Comparable interface. The reason is Java is strongly typed. Whenever a method is called, the responsibility of the compiler is to make sure that method exists. For example, if we see the example wherein we are calling the sort() method:
e.g.
In the above code the compiler should know that obj[i] has a compareTo() method defined. If obj is an array of Comparable object, then it is confirmed that the class has comapareTo() method defined in it, because every class that implements a Comparable interface provides the compareTo() method.
e.g.
AtrowelEmployeeSortEx.java
AtrowelEmployee.java
Properties of Interface
We must remember one thing: the interface is not a class. We cannot use a new operator to create an object of the interface.
e.g.
Even though we cannot create an object of the interface, we can declare the interface variables.
e.g.
The interface variable i.e. which we have declared in the above code must refer to the object of the class which implements that interface.
e.g.
As instanceOf is used to check whether the particular object is of the specific class, in the same way instanceOf is used to check whether an object implements an object. As we cannot use the instance fields in an interface, but we can provide constants in them. Now talking about the methods in the interface: the methods in an interface are public and the fields are public static final.
Each class has only one superclass, classes can implement multiple interfaces. It provides us with the maximum amount of flexibility in defining a class’s behavior. For example, Java has an important interface built-in called Cloneable. The concept of Cloneable is that if a class implements the Cloneable interface, the clone() method in the Object class will make an exact copy of the class’s objects. If we wish both cloneability and comparability, then we must implement both interfaces. We can use commas to separate the interfaces that we want to implement.
e.g.
Interfaces and Abstract Class in Java
We have already learned about the Abstract class. But you may be having a question: why the designers of Java have introduced the concept of the interface, and why Comparable cannot be used as an abstract class?
e.g.
In the above code the AtrowelEmployee class would simply extend this abstract class and supply the compareTo() method:
e.g.
A static or instance method might be a private method. Because private methods can only be used in the interface's methods, their use is limited to being helper methods for the interface's other methods. A class can only extend a single class. Suppose the AtrowelEmployee class already extends a different class, say, AtrowelManager. Then it cannot extend a second class.
e.g.
Each class is free to implement as many interfaces as it wishes.
e.g.
C++ allows the class to have more than one superclass. It is called multiple inheritance. However, the Java designers elected not to use multiple inheritance since it makes the language too complex or inefficient. Interfaces, on the other hand, provide the majority of the benefits of multiple inheritance while avoiding the complexities and inefficiencies.
Static and Private Methods
As per Java 8, we are allowed to use static methods for interfaces. It is common to place a static method in companion classes. In the standard library, we will find the pair of interface and utility classes like Collection/ Collections or Path/Paths. We can construct a path to a file or directory from URI or from a sequence of strings like Paths.get("jdk-11", "conf", "security"). In Java 11 methods are provided in the Path interface.
e.g.
Then the Path class is no longer necessary. In the same way, we can implement our own interfaces. There's no reason to have a separate companion class for utility methods anymore. As per Java 9, methods can be private in an interface. A static or instance method might be a private method. Because private methods can only be used in the interface's methods, their use is limited to being helper methods for the interface's other methods.