In this tutorial, we will learn about the Interfaces in Java. How to use them with the help of examples.

Define Interfaces in Java

Interface is pure-template class because it holds abstract method (no need to use abstract keyword) as well as final data. The Interface can be define using interface keyword. There is no individual meaning of interface class means we cannot create abject of the interface class. The importance of interface class with normal class by inherit it. This proves the concept of multiple inheritance because multiple inheritance can be inherited with a single class. Interface provides implements keyword for inheriting it with class. Interface is a always be public scope.

Syntax

interface <interface_name>{  
      
    // declare constant fields  
    // declare methods that abstract   
    // by default.  
} 
Syntax of Syntax

Why do use interface?

There are Following reasons to use java interface

  • By interface, We can support the functionality of multiple inheritance.
  • interface is use to achieve total abstraction.
  • interface can be use to achieve loose coupling.
  • Any class can extend only one class but can any class implement many number of interface.

Example of Interface

Input Value

interface company{  
  void print();  
  }  
  class abms implements company{  
  public void print(){System.out.println("Basic Engineer");}  
    
  public static void main(String args[]){  
  abms obj = new abms();  
  obj.print();  
   }  
  }  
Example of Interfaces in Java

Let’s run the code

Input Value

Output Value

Output Value

Multiple Inheritance in Java by Interface

We can achieve multiple inheritance through interfaces because interface contains only abstract method, which implementation is provided by the sub classes.

Note: class C implements A, B

Example of Multiple Inheritance in Java by Interface

Input Value

interface Company{  
  void print();  
  }  
  interface Study{  
  void show();  
  }  
  class abms implements Company,Study{  
  public void print(){System.out.println("Basic Engineer");}  
  public void show(){System.out.println("Hello Student");}  
    
  public static void main(String args[]){  
  abms obj = new abms();  
  obj.print();  
  obj.show();  
   }  
  }  
Example of Multiple Inheritance in Java by Interface

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *