Java Interface

 An interface is a group of related methods with empty bodies.

It is another way to achieve abstraction.

Implementation: To implement an interface, we use the keyword implements

Example 🡻

interface Bicycle {

//wheel revolutions per minute
void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);
}

To access the interface methods, the interface must be “implemented” (kinda like inherited) by another class with the implements keyword (instead of extends).

The body of the interface method is provided by the “implement” class:

Example 🡻

// Java program to demonstrate the 
// real-world example of Interfaces

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 GFG {

public static void main (String[] args) {

// creating an instance 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

 

The advantages of using interfaces in Java are as follows: ↴

🔴 It is used to achieve total abstraction.

🔴 Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.

🔴 Any class can extend only one class, but can implement multiple interfaces.

🔴 It is also used to achieve loose coupling.

🔴 Interfaces are used to implement abstraction. 

🔴 Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an “Vehicle ” object in the GFG Class)

🔴 Interface methods do not have a body – the body is provided by the “implement” class

🔴 On implementation of an interface, you must override all of its methods

🔴 Interface methods are by default abstract and public

🔴 Interface attributes are by default publicstatic and final

🔴 An interface cannot contain a constructor (as it cannot be used to create objects)

A class can extend another class, and similarly, an interface can extend another interface.

However, only a class can implement an interface, and the reverse (an interface implementing a class) is not allowed.

Multiple Inheritance is an OOPs concept that can’t be implemented in Java using classes.

But we can use multiple inheritances in Java using Interface.

let us check this with an example.

Example 🡻

// Java program to demonstrate How Diamond Problem
// Is Handled in case of Default Methods

// Interface 1
interface API {
    // Default method
    default void show()
    {

        // Print statement
        System.out.println("Default API");
    }
}

// Interface 2
// Extending the above interface
interface Interface1 extends API {
    // Abstract method
    void display();
}

// Interface 3
// Extending the above interface
interface Interface2 extends API {
    // Abstract method
    void print();
}

// Main class
// Implementation class code
class TestClass implements Interface1, Interface2 {
    // Overriding the abstract method from Interface1
    public void display()
    {
        System.out.println("Display from Interface1");
    }
    // Overriding the abstract method from Interface2
    public void print()
    {
        System.out.println("Print from Interface2");
    }

    // Main driver method
    public static void main(String args[])
    {
        // Creating object of this class
        // in main() method
        TestClass d = new TestClass();

        // Now calling the methods from both the interfaces
        d.show(); // Default method from API
        d.display(); // Overridden method from Interface1
        d.print(); // Overridden method from Interface2
    }
}


Output:

Default API
Display from Interface1
Print from Interface2

Example 🡻

interface FirstInterface {

  public void myMethod(); // interface method

}

interface SecondInterface {
  public void myOtherMethod(); // interface method

}

// DemoClass "implements" FirstInterface and SecondInterface
class DemoClass implements FirstInterface, SecondInterface {

  public void myMethod() {

    System.out.println("Some text..");

  }

  public void myOtherMethod() {

    System.out.println("Some other text...");

  }

}

class Main {

  public static void main(String[] args) {

    DemoClass myObj = new DemoClass();

    myObj.myMethod();

    myObj.myOtherMethod();

  }
}

Functional Interface

Marker Interface

Normal Interface