Tutorials

Java Object Oriented Programming


Interface


In java, interface provides complete abstraction.

In interface all methods are by default abstract*.  

 

*From java 8, interface can have static and default methods.

Just like abstract class, interface cannot be instantiated.

Variables in interface is always public static final. interface can only have constant variables.

 

Interface is declared using interface keyword in java.

 

Syntax:


interface SampleExample{

   public static final int constVar = 9; 
   public void method1();

}

implements keyword

Any classes that wishes to use the interface must implement it and also provide implementation to all of its abstract methods. 

implements keyword is used to implement an interface in java.

 

Example:


Class A implements SampleExample { 

    public void method1( ){
        System.out.Println("Interface implementation provided")
    }

     public void method2( ){
          System.out.Println("Some more tasks by A class")
     }
}

Source: MainClass.java

public class MainClass {

    public static void main(String[] args) {

    A a = new A();
    a.method1();
    a.method2();

    }

}

OUTPUT:

Interface implementation provided

Some more tasks by A class

 

What if the implementing class doesn't provide implementation to one or more of the abstract methods of interface?

If the class implementing interface doesn't provide implementation to one or more of the abstract method of the interface then that class must be declared abstract. 

 

If implementing class doesn't provide implementation to any of the abstract method of the interface and is also not declared abstract then compiler will show error.

In the above  example had class A not provided implementation to class SampleExample's abstract method 'method1()' we would have been forced to declare class A as abstract. 

Otherwise we will get compiler error with message: "The type A must implement the inherited abstract method SampleExample.method1()"

 

See the below example, where class A is declared abstract as it doesn't provide implementation to method1() of interface SampleExample.

 

abstract class A implements SampleExample { 

    //method1 not implemented 

    public void method2(){
         System.out.println("Some more tasks by A class");
    }

}

 

Why interface is required?

It's a good question to think about.

If interface can only contain abstract methods and constant variables (public static final) then what is at all good about them.

 

Think of interface as a contract where class implementing it enters into agreement to provide implementation to all of its abstract methods. Inteface only defines what needs to be done and leaves how to do it on the implementing classes. 

Once the class implements an interface then it needs to provide implementation to all of the interface's unimplemented methods.If implementing class wishes not to provide implentation to any of the abstract method of interface then that class should be declared abstract.

 

How interface support multiple inheritance?

A class in java can extend only one class so multiple inheritance is restricted for a class. But there is no such restriction on a class implementing interface.

One class can implement multiple interfaces. An interface is free to extend multiple interface.

Multiple inheritance with interface by tutorials in hand

 

Things to keep at your finger tips:

  • Class implements an interface(s).
  • Class extends a class.
  • Interface extends interface(s)

Thus interface in java helps to support multiple inheritance indirectly.



Please Share this page
Views : 119
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!