Interface in Java

2 min 51 sec read Basic


An interface provides full abstraction whereas abstract class provides partial abstraction. Full abstraction means interface does not contain concrete methods. It is made up of only abstract methods.

Rules:

1) Multiple inheritance in Java is possible using interface. Yes, the class can extend any number of interfaces.

2) The class which implements an interface must implement all the abstract methods in an interface.

3) All the variables inside interface are public, final and static which means you cannot change the value of variables in the extended interface.

4) All the methods in an interface are abstract and public by default.

5) Interface contains only abstract methods. It does not contain concrete methods whereas abstract methods contain both abstract as well as concrete methods(normal methods).

Basic Syntax

An interface is written using interface keyword.

interface YourInterfaceName   
  {
   // Declare  abstract methods here.
  }

Java Interface Example

In this Java Interface example., we will create two interface StudentInterface and CollegeInterface and implement it in class named as InterfaceDemo.

interface StudentInterface
  {
     public void displayStudentInfo();
  }
  
  interface CollegeInterface extends StudentInterface
  {
         public void displayCourseInfo();
  }

  class InterfaceDemo implements CollegeInterface
  {
      // Abstract methods implementation
      public void displayStudentInfo()
      {
          System.out.println("Display Student Info");
      }
      
      public void displayCourseInfo()
      {
          System.out.println("Display Course Info");
      }
      
   
public static void main (String[] args) 
throws java.lang.Exception
    {
      CollegeInterface c = new InterfaceDemo();
// Calling parent interface method with child 
interface reference.
      c.displayStudentInfo();      
    }
  }

Output:

Display Student Info

We created two interfaces named StudentInterface and CollegeInterface. StudentInterface is a parent interface whereas CollegeInterface is the child interface. Now, even tough class InterfaceDemo implements only CollegeInterface. You have to implement abstract method from Parent interface too else compilation error will be thrown.

Example:

Let's see how interface supports multiple inheritance in Java with an example. Multiple interfaces can be implemented by writing their names after class and they should be separated by the comma.

interface StudentInfo
  {
     public void studentInfo();
  }
  interface CollegeInfo
  {
     public void collegeInfo();
  }
  
  class InterfaceDemo implements StudentInfo,CollegeInfo
  {
    // Abstract methods implementation
     public void studentInfo()
     {
          System.out.println("Student Info");
     }
     
    public void collegeInfo()
     {
          System.out.println("Student Info");
     }
     
     public static void main(String args[])
     {
          InterfaceDemo c = new InterfaceDemo();
          c.studentInfo();
      }
  }

Output:

Student Info

Why to use interface in Java?

As we discussed, multiple inheritance was not possible with class in Java which can be solved using an interface in Java and also it provides better security as variables are by default final and static.

 

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python