Categories
Java Tutorials

Interfaces in Java & inheritance among interfaces in Java

Learn what are Java interfaces, and how multiple inheritance is incorporated in Java with the help of Java interfaces.

interface SimpleInterface 
{
   void useInterfaceMethod(int i); 
}

Main Features of Java Interfaces

  • Interfaces can only holds constants and abstract methods. Only methods signature is allowed without body.
  • Since Java8 default methods are allowed inside Interface.
  • You may or may not declare variables inside interface, its optional.
  • You may or may not declare method signature inside interfaces. Its optional.
  • Normal variables declared inside interfaces, to be treated as final or static variables, so they cannot be changed by implementing class.
  • You need not to write abstract keyword before a method inside interfaces.
accessmodifier interface interface_name {
    returntype methodname( parameters list comma separated );
}

How a class implement an interface – with code example

interface SimpleInterface
{
    void display(String s);
}

Class NewClass implements SimpleInterface
{
    public void display(String s) {
         System.out.println(s);
    }
}

Once an interface has been defined, then one or more classes can implement that interface. But implementing classes must override the methods declared inside the interface.

Implementing Interfaces : How a class use interfaces

  • A class can use any number of interface by using the implements keyword.
  • If there exists more than one interfaces then the class should use, (comma) to separate them.
  • When you implement an interface (s) then you should override interface methods.
  • In the implementing class file, the overridden methods to be declared as public, which might not be public in the mother interface.

A class implement(s) multiple interfaces

File : Interface2.java

interface Interface {
        float calculateAvg(int a, int b); 
}

File : Interface1.java

interface Interface1 { 
             int calculateSum(int a, int b); 
}

File: ClassImplementInterface1and2.java

class ClassImplementInterface1and2 implements Interface1,
                                          Interface2
{
    @Override
    public float calculateAvg(int a, int b) {
              return (a+b)/2;
    }

    public static void main( String[] args ) 
    {
          ClassImplementInterface1and2 obj = new ClassImplementInterface1and2();
          int x=10;
          int y=20;
          System.out.println( "Avg is = " + obj.calculateAvg(x,y) );
          System.out.println( "Sum is = " + obj.calculateSum(x,y) );
     }

     @Override
     public int calculateSum(int a, int b) {
            return a+b;
     }
}

Output of the program :
Avg is = 15.0
Sum is = 30

The implementing class should override all the methods of each interface.

If these interfaces have super interface, then the class should implement all methods of super interfaces.


In the implementing class methods should be public

  • During overriding of a method in a class implementing an interface, inside class the overridden method should be declared as public, even if it was not public in the interface.

Inheritance among interfaces, just like class

ParentInterface.java

public interface ParentInterface {
            void displayParent();
            String PARENT = " I am member inside Parent Interface ";
            int PARENTSALARY = 10000;
}

ChildInterface.java

public interface ChildInterface extends ParentInterface {
      void displayChild();
      int CHILDSALARY = 50000;
}

MainClassImplementsChildInterface.java

/**
* @author Bikram Choudhury
* Date : 29/05/2018
*/
    public class MainClassImplementsChildInterface implements ChildInterface {

    public static void main(String[] args) {
       new MainClassImplementsChildInterface().displayChild();
       new MainClassImplementsChildInterface().displayParent();
    }

    @Override
    public void displayChild() {
       System.out.println("CHILDSALARY integer ( Child Interface Member Variable ), value = " + CHILDSALARY);
    }

    @Override
    public void displayParent() {
       System.out.println("PARENT String value ( Parent Interface Member variable )" + PARENT + " , PARENTSALARY => " + PARENTSALARY);
    } 
}

The main implementing class implements both parent and child interface methods. Implementing class cannot change the variables in the interfaces PARENT, PARENTSALARY and CHILDSALARY. These members are final and cannot be changed in implementing classes.

JDK 8 Updates on Interfaces

It is possible to define the default behaviour of a method declared in an interface. This is called default method. A Non-default interface method is implicitly abstract.