Java Interface With Examples

What is an interface?

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:

  1. De-couple the declaration and implementation to attain full abstraction.
  2. To have a workaround for multiple inheritances.

We mentioned, the interface is similar to a class but it has differences as well.

  1. The interface cannot be instantiated and is implicitly abstract.
  2. The data members are static and final.
  3. The methods are implicitly abstract and public, of which only method signature is given.
  4. The interface is implemented, not extended.
  5. Multiple interfaces can be implemented.

Interface declaration

The syntax of the interface declaration is :

interface <InterfaceName>{
//Constants
//Method Signature
//Default Method
//Static method
}

Classes Implement Interfaces

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.

Example:

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();
    }
}

Output:

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.

Override the method

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();
    }
}

Output:

I am implemented inside MyClass

Declare the class abstract

interface MyInterface{
   void methodToImplement();
}

abstract class MyClass implements MyInterface{
} 

public class ClassMain{
    public static void main(String args[]){      
    }
}

Interface extends Interfaces

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.

Syntax:

//Single Parent Interface
interface myInterface extends myParentInterface{

}

//Multiple Parent Interface
interface myInterface extends myParentInterface1, myParentInterface2{

}

Example:

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();
    }
}

Output:

I am Parent Interface Method.
I am Child Interface Method.

Example 2: interface extending multiple interfaces

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();
}
}

Output:

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.