dsa

Java Interfaces

Introduction :

In this tutorial, we will learn about Java interfaces. We will learn how to implement interfaces and when to use them in detail with the help of examples.

What is an Interface ?

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

Java Interface also represents the IS-A relationship.

It cannot be instantiated just like the abstract class.

Since Java 8, we can have default and static methods in an interface.

Since Java 9, we can have private methods in an interface.

Why to use Java Interface :

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

  1. It is used to achieve abstraction.
  2. By interface, we can support the functionality of multiple inheritance.
  3. It can be used to achieve loose coupling.

Syntax :


interface <interface_name>
{  
      
    // declare constant fields  
    // declare methods that abstract   
    // by default.  
}  

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword.

Example of an Interface :


// A simple interface 
interface Player 
{ 
	final int id = 10; 
	int move(); 
} 

implements Keyword in Interface

Like abstract classes, we cannot create objects of interfaces. However, we can implement interfaces in other classes. In Java, we use the implements keyword to implement interfaces. For example,


// Java program to demonstrate working of 
// interface. 
import java.io.*; 

// A simple interface 
interface In1 
{ 
	// public, static and final 
	final int a = 10; 

	// public and abstract 
	void display(); 
} 

// A class that implements the interface. 
class TestClass implements In1 
{ 
	// Implementing the capabilities of 
	// interface. 
	public void display() 
	{ 
		System.out.println("CodeMistic !"); 
	} 

	// Driver Code 
	public static void main (String[] args) 
	{ 
		TestClass t = new TestClass(); 
		t.display(); 
		System.out.println(a); 
	} 
} 
Output :

CodeMistic !
10

In the above program, we have created an interface In1. The In1 interface has an abstract method display(). This means that any class that implements In1 must provide an implementation for the display() method. Notice that, the TestClass class (which implements In1 interface) has the method display() with implementation.

New Features added in interfaces in JDK 9

From Java 9 onwards, interfaces can contain following also :

  1. Static methods
  2. Private methods
  3. Private Static methods

extends Keyword in Interface

Similar to classes, interfaces can extend other interfaces. The extends keyword is used for extending interfaces. For example,


interface A 
{
   ...
}
interface B 
{
   ... 
}

Interface C extends A, B 
{
   ...
}

A real world Example


import java.io.*; 

interface Vehicle 
{ 
	
	// all are the abstract methods. 
	void changeGear(int a); 
	void speedUp(int a); 
	void applyBrakes(int a); 
} 

class Bicycle implements Vehicle
{ 
	
	int speed; 
	int gear; 
	
	// to change gear 
	@Override
	public void changeGear(int newGear)
	{ 
		
		gear = newGear; 
	} 
	
	// to increase speed 
	@Override
	public void speedUp(int increment)
	{ 
		
		speed = speed + increment; 
	} 
	
	// to decrease speed 
	@Override
	public void applyBrakes(int decrement)
	{ 
		
		speed = speed - decrement; 
	} 
	
	public void printStates() { 
		System.out.println("speed: " + speed 
			+ " gear: " + gear); 
	} 
} 

class Bike implements Vehicle 
{ 
	
	int speed; 
	int gear; 
	
	// to change gear 
	@Override
	public void changeGear(int newGear)
	{ 
		
		gear = newGear; 
	} 
	
	// to increase speed 
	@Override
	public void speedUp(int increment)
	{ 
		
		speed = speed + increment; 
	} 
	
	// to decrease speed 
	@Override
	public void applyBrakes(int decrement)
	{ 
		
		speed = speed - decrement; 
	} 
	
	public void printStates() { 
		System.out.println("speed: " + speed 
			+ " gear: " + gear); 
	} 
	
} 
class CodeMistic { 
	
	public static void main (String[] args) 
	{ 
	
		// creating an inatance of Bicycle 
		// doing some operations 
		Bicycle bicycle = new Bicycle(); 
		bicycle.changeGear(2); 
		bicycle.speedUp(3); 
		bicycle.applyBrakes(1); 
		
		System.out.println("Bicycle present state :"); 
		bicycle.printStates(); 
		
		// creating instance of the bike. 
		Bike bike = new Bike(); 
		bike.changeGear(1); 
		bike.speedUp(4); 
		bike.applyBrakes(3); 
		
		System.out.println("Bike present state :"); 
		bike.printStates(); 
	} 
} 

Output :

Bicycle present state :
speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1