Interface in Java

Article by: Manish Methani

Last Updated: October 26, 2021 at 8:04am IST
2 min 51 sec read

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.

 

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com