Introduction to Interface In Java


In this article, we will discuss the interface in Java. As multiple inheritances in Java only achieve through Interface. So we discuss Interface in Java.

What is Interface?

  • In computing, the interface acts like a communicator between peripheral devices(monitor, keyboard) and computer system.
  • The infield of computer science, an interface refers to an interactor between components.
  • In computer any type of communication of computer system with the user we say it is an interface.

Interface in Java

An interface is a blueprint of a class. It is a java core part and a way to achieve data abstraction in Java along with the abstract class. Since multiple inheritances are not allowed in Java, the interface is the only way to implement multiple inheritances. At in basic level interface in java is a keyword but that time it is an object-oriented term to define contracts and abstraction, This contract is followed by any implementation of Interface in Java.
Key Point of Interface in JAVA
  • You can fully abstract a class using keyword interface.
  • While declaring an interface in a class there is no need for a keyword abstract.
  • All methods in an interface are implicitly public.
  • The interface is not a class defining the interface is similar to defining a class, but they are two different concepts. A class defines the attributes and behaviors of an object. An interface contains behaviors that a class implements.
  • We have to define all the abstract methods of the interface in the class.

Declaring Interfaces

Using the Interface keyword to declare an interface.
Define a simple interface
  1. interface Ex1  
  2. {  
  3.     public static final int min=10;  
  4.     public abstract void print();  
  5. }  
Define another interface
  1. interface Ex2  
  2.  {  
  3.     public static final int min=5;  
  4.     public abstract void show();  
  5.  }  

Implementing Interfaces

A class uses the implements keyword to implement an interface. If a class implements more than one interface, they are separated with a comma. The methods that implement an interface must be declared public (to be accessed by other members). Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition.
Sample Program
  1. class InheritanceEx implements Ex1  
  2.  {  
  3.     public void print()  
  4.       {  
  5.         System.out.println("Hello");  
  6.       }  
  7.     public static void main(String args[])  
  8.       {  
  9.         InheritanceEx obj=new InheritanceEx();  
  10.         obj.print();  
  11.       }  
  12.  }  

Multiple Inheritance

Multiple inheritances are the ability of a single class to inherit from multiple classes. Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit characteristics and features from more than one superclass. It is distinct to single inheritance, where a class may only inherit from one particular superclass.
Java does not support multiple inheritances. For achieving multiple inheritance JAVA use interfaces.
In the below figure, we show Multiple Inheritance. This figure contains multiple animal class, this animal class are inherited according to there nature (like Rabbit in Herbivore, Lion and Hyena in Carnivore), and after that, all these class inherited in a single class called Animal Class.
Implementing Multiple Interfaces (i.e Multiple Inheritance)
Sample program
  1. class MulInheritanceEx implements Ex1,Ex2  
  2. {  
  3.     public void print()  
  4.       {  
  5.         System.out.println("Hello");  
  6.       }  
  7.     public void show()  
  8.       {  
  9.         System.out.println("Welcome");  
  10.       }  
  11.     public static void main(String args[])  
  12.       {  
  13.         MulInheritanceEx obj=new MulInheritanceEx();  
  14.         obj.print();  
  16.       }  
  17. }  
Note 1-For overriding methods there are certain rules to be followed
  1. The argument the parameter should be exactly the same as that of the overridden method.
  2. A method declared static cannot be overridden but can be re-declared.
  3. The return type should be the same or a subtype of the return type declared in the original an overridden method in the superclass.
  4. A method declared final cannot be overridden.
  5. Constructors cannot be overridden.
  6. If a method cannot be inherited then it cannot be overridden.
  7. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.


In this article, we learned about Introduction to Interface In Java.