In Java, an interface
is a reference type similar to a class. It is a collection of constants, method signature(implicitly abstract), the default method (introduced in Java 8), static method, and nested types. The method body/implementation of only the default and static method is applicable inside the interface.
The purpose of the interface is as follows:
We mentioned, the interface is similar to a class but it has differences as well.
The syntax of the interface declaration is :
interface <InterfaceName>{
//Constants
//Method Signature
//Default Method
//Static method
}
A class can implement any number of interfaces. The keyword implements
is used to indicate that an interface(s) is/are implemented during class declaration. Implementing an interface makes it mandatory to implement all the method signatures. If even a single method cannot be implemented, the class is to be declared as an abstract
class. This guarantees that all of the implicit abstract methods will be implemented at any level of inheritance.
Here we see that MyClass implements MyInterface. There is a method methodToImplement() that is required to be overridden. But since MyClass does not override the same, we get an error.
interface MyInterface{
void methodToImplement();
}
class MyClass implements MyInterface{
}
public class ClassMain{
public static void main(String args[]){
MyClass obj = new MyClass();
}
}
ClassMain.java:5: error: MyClass is not abstract and does not override abstract method methodToImplement() in MyInterface
class MyClass implements MyInterface{
^
1 error
So we have two options. Either we override the method, or we declare the class as abstract
.
interface MyInterface{
public void methodToImplement();
}
class MyClass implements MyInterface{
@Override
public void methodToImplement(){
System.out.println("I am implemented inside MyClass");
}
}
public class ClassMain{
public static void main(String args[]){
MyClass obj = new MyClass();
obj.methodToImplement();
}
}
I am implemented inside MyClass
interface MyInterface{
void methodToImplement();
}
abstract class MyClass implements MyInterface{
}
public class ClassMain{
public static void main(String args[]){
}
}
An interface
can extend another interface
just like class extends
another class. As an impact, the methods of parent interface get inherited to child interface. In the case of interface, it is possible to extend multiple interfaces.
//Single Parent Interface
interface myInterface extends myParentInterface{
}
//Multiple Parent Interface
interface myInterface extends myParentInterface1, myParentInterface2{
}
We have MyChildInterface that extends a single interface MyParentInterface. As a result, MyChildInterface has 2 methods to be implemented i.e methodChild(own method), methodParent(inherited method). MyClass implements MyChildInterface and overrides both the methods mentioned.
interface MyParentInterface{
public void methodParent();
}
interface MyChildInterface extends MyParentInterface{
public void methodChild();
}
class MyClass implements MyChildInterface{
public void methodParent(){
System.out.println("I am Parent Interface Method.");
}
public void methodChild(){
System.out.println("I am Child Interface Method.");
}
}
public class ClassMain{
public static void main(String args[]){
MyClass obj = new MyClass();
obj.methodParent();
obj.methodChild();
}
}
I am Parent Interface Method. I am Child Interface Method.
Here MyClass implements multiple interfaces i.e FatherInterface and MotherInterface. As a result, it overrides both the unimplemented method.
interface FatherInterface{
public void methodFather();
}
interface MotherInterface{
public void methodFather();
}
class MyClass implements FatherInterface,MotherInterface{
public void methodFather(){
System.out.println("I am Father Interface Method.");
}
public void methodMother(){
System.out.println("I am Mother Interface Method.");
}
}
public class ClassMain{
public static void main(String args[]){
MyClass obj = new MyClass();
obj.methodFather();
obj.methodMother();
}
}
I am Father Interface Method.
I am Mother Interface Method.
This was an overview of Java Interface. We will discuss the important comparison between Abstract Class and Interface in the next lecture.