Interface in Java

In this tutorial, we will discuss Interface in Java. We will understand what they are, how to implement them, why they are used in Java programming and much more. So, let’s begin with its introduction.

Interface in Java

An Interface is a fundamental concept in Java programming. It is similar to a class but with some differences.

interface in java

  • An Interface is a group of abstract methods and constants.
  • An Interface may contain constants, static methods, default methods, and nested types.
  • Default and static methods have their body in an interface.
  • An Interface cannot be instantiated.

A class mainly describes the behaviours for the object that is instantiated using it. But an interface contains behaviours that a class must implement if it implements it. If the class that implements the interface is not abstract, then all the methods of the interface need to be defined in the class or the program won’t compile.

Syntax

interface <name> {
    // constant 1
    // constant 2, so on

    // abstract method 1
    // abstract method 2, so on
}

Example

interface MySimpleInterface {
    void hello();
}

How to implement Interface in Java?

Using an Interface is just like a signed contract between a class and an Interface. The class that implements an Interface needs to agree to perform specific behaviours of the interface and if that class does not perform all the behaviours, then the class must declare itself as abstract or again the program won’t run. This might seem a bit complicated, but it isn’t.

The ‘implements’ keyword is used in Java to implement an interface. This keyword appears in the class declaration after the ‘extends’ part of the declaration.

Example

interface Wifi {
    void connect();
    void disconnect();
}

class Mobile implements Wifi {

    public void connect() {
        System.out.println("Wifi connected");
    }

    public void disconnect() {
        System.out.println("Wifi disconnected");
    }

    public static void main(String args[]) {
         Mobile mobile = new Mobile();
        mobile.connect();
        mobile.disconnect();
    }
}

//Output
//Wifi connected
///Wifi disconnected

Why should I use Interface in Java?

  • Abstraction is achieved using Interface.
  • To achieve loose coupling.
  • Supports the functionality of multiple inheritances.

There are few things to keep in mind when using an Interface in Java

  • The same signature must be used when overriding interface methods.
  • Interface methods must be implemented in classes that are not abstract.
  • Zero, one or more interfaces can be implemented by a class at a time.
  • An interface can extend another interface just how a class extends another class.

Extending an Interface in Java

An interface can extend another interface just like a class that extends another class. To extend an Interface, the ‘extends’ keyword is used.

Example

interface HotSpot {
    void enableHotspot();
    void disableHotspot();
}

interface WirelessFeatures extends HotSpot {
    void connectBluetooth();
    void disconnectBluetooth();
}

class Smartphone implements WirelessFeatures {

    public void connectBluetooth() {
        System.out.println("Bluetooth connected");
    }

    public void disconnectBluetooth() {
        System.out.println("Bluetooth disconnected");
    }

    public void enableHotspot() {
        System.out.println("Hotspot enabled");
    }

    public void disableHotspot() {
        System.out.println("Hotspot disabled");
    }

    public static void main(String args[]) {
        Smartphone smartphone = new Smartphone();
        smartphone.connectBluetooth();
        smartphone.enableHotspot();
        smartphone.disconnectBluetooth();
        smartphone.disableHotspot();
    }
}

//Output
// Bluetooth connected
// Hotspot enabled
// Bluetooth disconnected
// Hotspot disabled

In the above example, The interface WirelessFeatures is extending the Hotspot interface. Now the WirelessFeatures has a total of 4 abstract methods in it. The class Smartphone implements the WirelessFeatures interface so it must implement all the 4 abstract methods that the interface contains.

Extending Multiple Interfaces

A Java class can only extend one class as its parent because multiple inheritances are not allowed in Java. Interfaces are not classes so an interface can extend more than one interface at a time. Each of the parent interfaces must be comma-separated.

extending multiple interfaces

Example

interface NFC {
    void enableNFC();
    void disableNFC();
}

interface Internet {
    void enableInternet();
    void disableInternet();
}

interface Wireless extends NFC, Internet {
    boolean isWireless();
}

class Smartphone implements Wireless {

    public void enableNFC() {
        System.out.println("NFC enabled");
    }

    public void disableNFC() {
        System.out.println("NFC disabled");
    }

    public void enableInternet() {
        System.out.println("Internet enabled");
    }

    public void disableInternet() {
        System.out.println("Internet disabled");
    }

    public boolean isWireless() {
        return true;
    }

    public static void main(String args[]) {
        Smartphone smartphone = new Smartphone();
        System.out.println("Wireless: " + smartphone.isWireless());
        smartphone.enableNFC();
        smartphone.disableNFC();
        smartphone.enableInternet();
        smartphone.disableInternet();
    }
}

//Output
// Wireless: true
// NFC enabled
// NFC disabled
// Internet enabled
// Internet disabled

I hope at this point you have a better understanding of an Interface in Java. To learn more on various topics of Java keep following us. Thank you for reading.