Interfaces in Java

Interface is a pure abstract class.They are syntactically similar to classes, but you cannot create instance of an Interface and their methods are declared without any body. Interface is used to achieve complete abstraction in Java.

When you create an interface it defines what a class can do without saying anything about how the class will do it.

Java Interface also represents IS-A relationship.

It cannot be instantiated just like abstract class.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

  • It is used to achieve fully abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.
Note : The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.

However, an interface is different from a class in several ways, including:

  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

Rules for using Interface

  • Methods inside Interface must not be static, final, native or strictfp.
  • All variables declared inside interface are implicitly public static final variables(constants).
  • All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
  • Interface can extend one or more other interface.
  • Interface cannot implement a class.
  • Interface can be nested inside another interface.

Understanding relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.

interface

Example of Java interface

interface printable {
	void print();
}

class A implements printable {
	public void print() {
		System.out.println("Hello");
	}

	public static void main(String args[]) {
		A obj = new A();
		obj.print();
	}
}
 
 	Output:Hello
 	

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.

interface2
 interface Printable {
	void print();
}

interface Showable {
	void show();
}

class A implements Printable, Showable {

	public void print() {
		System.out.println("Hello");
	}

	public void show() {
		System.out.println("Welcome");
	}

	public static void main(String args[]) {
		A obj = new A();
		obj.print();
		obj.show();
	}
}
Output:Hello
       Welcome
       

Q) Multiple inheritance is not supported through class in java but it is possible by interface, why?

As we have explained in the inheritance chapter, multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class

Interface inheritance

A class implements interface but one interface extends another interface .

Nested Interface in Java

Note: An interface can have another interface i.e. known as nested interface. We will learn it in detail in the nested classes chapter. For example:
  interface printable{  
 void print();  
 interface MessagePrintable{  
   void msg();  
 }  
} 

Difference between abstract class and interface

Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated. differences between abstract class and interface .

Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor.
5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
7) Example:
public class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}