What is Interface in Java
An interface in Java is a mechanism which is used to achieve completely abstraction. It is basically a kind of class but can have only abstract methods declaration and constants as members.
In other words, an interface in Java is a collection of abstract methods and constants (i.e, final fields). Every interface is by default abstract.
So, It is not compulsory to write abstract keyword with the interface. We cannot instantiate an interface just like an abstract class but we can create a reference variable.
Why Use Java Interface?
There are mainly five reasons to use the interface. They are as follows:
1. In industry, architect level people create interfaces and then it is given to developers for writing classes by implementing interfaces provided.
2. Using interfaces is the best way to expose our projects API to some other project.
For example, HDFC bank can expose methods or interfaces to various shopping carts.
3. Programmers use interface to customize features of software differently for different objects.
4. It is used to achieve fully abstraction.
5. By using interface, we can achieve the functionality of multiple inheritance.
How to Declare Interface in Java?
In Java, an interface is declared much like a class. It is declared by using the keyword interface followed by the interface name. It has the following general form:
accessModifier interface interfaceName
// declare constant fields.
// declare methods that abstract by default.
public interface MyInterface
int x = 10; // For simplicity, Integer constant are used.
int y = 20:
void m1(); // public and abstract keyword invisibly present.
As you can see in above example, both methods m1() and m2() in an interface are declared with no body and does not have public or abstract modifiers present. The variables x and y declared in MyInterface are like simple variables.
The compiler automatically adds public and abstract keyword before to all interface methods. Moreover, it also adds public, static and final keyword before interface variables. It is shown in the below figure.
Therefore, all the variables declared in an interface are considered as public, static and final by default and acts like constant. We cannot change their value once they initialized.
1. The value of interface variables must be initialized when it is declared. Since it is defined as final.
2. A class that implements an interface, must provide implementations of all the methods defined in the interface.
3. We cannot reduce the visibility of an interface method while overriding.
Features of Interface in Java
There are following features of the interface in Java. They are as follows:
4. The fields (data members) declared in an interface are by default public, static, and final. Therefore, they are just public constants. So, we cannot change their value by implementing class once they are initialized.
5. Interface cannot have constructor.
6. It can be compiled but cannot run.
7. The interface is the only mechanism which allows achieving multiple inheritance in java.
8. A Java class can implement any number of interfaces by using keyword implements.
9. Interface can extend an interface and can also extend multiple interfaces.
10. If you add any new method in interface, all concrete classes which implement that interface must provide implementations for newly added method because all methods in interface are by default abstract.
Extending Interfaces in Java with Example
Like classes, Interface can also extend another interface. That means an interface can be sub interfaces from other interfaces. The new sub interface will inherit all members of the super interface similar to sub classes. It can be done by using a keyword “extends”. It has the following general form:
You can also add various interfaces together into a single interface by the following declaration.
1. An interface cannot extend classes because it would violate rule that an interface can have only abstract method and constants.
2. An interface can extend Interface1, Interface2.
Implementing Interface in Java with Example
An interface is used as “super class” whose properties are inherited by a class. A class can implement one or more than one interface by using a keyword implements followed by a list of interfaces separated by commas.
When a class implements an interface, it must provide an implementation of all methods declared in interface and all its super interfaces. Otherwise, the class must be declared as abstract. The syntax of interface implementation using a class is shown below.
Accessing Interface Variables in Java
The interface is also used to declare a set of constants that can be used in multiple classes. The constant values will be available to any classes that implement interface because it is by default public, static, and final. The values can also be used in any method as part of the variable declaration or anywhere in the class.
Let’s see various types of example programs related to all important points.
Java Interface Example Programs
Let’s create a program where multiple classes implement the same interface to use constant values declared in that interface.
Program source code 1:
Let’s create a program where class B implements an interface A.
Program source code 2:
Polymorphism in Interfaces
Let’s create a program where multiple classes implement the same interface. When two or more classes implement the same interface with different implementations then through the object of each class, we can achieve polymorphic behavior for a given interface. This is called polymorphism in interfaces.
In the above figure, (d) shows polymorphism in interfaces where class B and class C implement the same interface A.
Program source code 3:
Multilevel Inheritance by Interface
Let’s make a program where we will create a multilevel inheritance by interface. One interface extends an interface, that interface extends another interface, and a class implements methods of all interfaces, we can achieve multilevel inheritance by interface.
Program source code 4:
Multiple Inheritance in Java by Interface
When a class implements more than one interfaces, or an interface extends more than one interfaces, it is called multiple inheritance. Various forms of multiple inheritance are shown in the following figure.
Let’s create a program to achieve multiple inheritance using multiple interfaces.
Program source code 5:
In Java, Multiple inheritance is not supported through class but it is possible by an interface, why?
As we have explained in the inheritance chapter, in multiple inheritance, sub classes are derived from multiple super classes. If two super classes have the same method name then which method is inherited into subclass is the main confusion in multiple inheritance.
That’s why Java does not support multiple inheritance in case of class. But, it is supported through an interface because there is no confusion. This is because its implementation is provided by the implementation class.
Let’s understand it with a suitable example program.
Since both methods in interfaces have no body, and the body is provided in the implementation class i.e, its implementation is provided by class Myclass. Thus, we can use methods of AA and BB interfaces without any confusion in a subclass.
Java 8 Default method in Interface
Since Java 1.8 or above, Default methods are allowed where we can have method body in interface. But we will need to make it default method. Let’s see an example program related to it.
Let us take an example program where we will declare a default method with a body in interface.
Program source code 7:
Java 8 Static Method in Interface
Since Java 1.8 or above, static methods are allowed in interface. Let’s see an example.
Can We have an Interface without any Methods or Fields?
An interface without any fields or methods is called marker interface. There are several built-in Java interfaces that have no method or field definitions. For example, Serializable, Cloneable, and Remote all are marker interfaces. They act as a form of communication among class objects.
Hope that this tutorial has covered almost all important points related to interface in java with example programs. I hope that you will have understood java interface and enjoyed its programming.
Thanks for reading.