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.

 

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>