JAVA - All about Interfaces

Photo by Luca Bravo on Unsplash

JAVA - All about Interfaces

In JAVA, Multiple Inheritance is supported in the form of Interfaces. An interface is a type of reference that defines a set of abstract methods, which are methods without any implementation. So, Whoever the Child Class is implementing the interface they have to give the method definition. In this blog, we will explore the concept of interfaces in Java, their uses, and how to implement them in your code.

Why use Interfaces?

With the help of interfaces, we can achieve something called Abstraction. So, What is Abstraction? Abstraction means hiding the actual implementation of what is happening in the behind scenes, we are hiding it. Example : Riding the bike. As per our knowledge when we insert the keys into the bike and do a self start or kicker our bike will start, that's what we know. But we don't know what is happening inside at the engine level. So, Abstraction is about Hiding unnecessary details and showing only valuable information. It focuses on the external stuff.

In Java, We can achieve Abstraction in 2 ways.

  • By using Interfaces.

  • By Abstract Classes.(will see it in the future blogs)

Let's See how we can implement interfaces in our code.

interface Bike {
    public void speed();
    public String bikeColor();
    public boolean isElectric();
}

In the above code, We have a Bike interface with 3 unimplemented methods. In this case, it's the child class's responsibility to provide implementation.

class Ducati implements Bike {
    @Override
    public void speed() {
        System.out.println("Ducti's top speed is 250KMPH");
    }
    @Override
    public String bikeColor() {
        String colorOfBike = "Red";
        return colorOfBike;
    }
    @Override
    public boolean isElectric() {
        System.out.println("Ducati not a electric bike");
        return false;
    }
}

class Ather implements Bike {
    @Override
    public void speed() {
        System.out.println("Ather's top speed is 90KMPH");
    }
    @Override
    public String bikeColor() {
        String colorOfBike = "White";
        return colorOfBike;
    }
    @Override
    public boolean isElectric() {
        System.out.println("Ather is an electric bike");
        return true;
    }
}

In this above example, we are having two classes Ducati and Ather providing implementation to the bike interface. Please note that the class must provide an implementation for all the methods in the interface.

Types of Interfaces

There are three types of interfaces

  • Functional interface

  • Normal interface

  • Marker interface

Functional interface

A functional interface is an interface where we have only one abstract method is called a functional interface. Let's see the below example.

@FunctionalInterface
interface Demo {
    public void fInterfaceExample();
}

A functional interface can be implemented using classes, lambda expressions and anonymous inner classes. Lambda expression and anonymous inner class we'll see it in the upcoming blog. Let's see how we can implement it in the form of a class.

class Functional implements Demo{
    @Override
    public void fInterfaceExample() {
        System.out.println("Implementation is given");
    }
}

Normal interface

It is a type of interface where we can have two or more abstract methods. As we have seen in the above example (Bike) interface.

Marker interface

It is an interface that has no methods. It's a blank interface. It provides run-time type information about objects, so the compiler and JVM have additional information about the object. It is also called as tagging interface. In java, we are having many built-in marker interfaces such as Serializable, Cloneable, and Remote.

Changes in interfaces After Java 8

As we have seen, in interfaces we do not have a method body. All the methods are abstract methods. But here's a shock! After Java 8, in interface, we can have method with body. We have to use it with "default" keyword. This default keyword will not behave like an access specifier. Let's see the below example.

interface Example {
    public default void demo01() {
    System.out.println("default methods are allowed in interface");
    }
    public static void demo02() {
    System.out.println("static methods are allowed in interface");
    }
    public void demo03();
}

The default methods will participate in inheritance. Also, it can be overridden by the implementing classes. Static methods are also allowed but they can't be overridden. We can directly call static methods with the help of "Interfacename.methodName();". From Java 9 onwards, we can also have private methods in an interface. Let's see with the example.

interface Example {
    public default void demo01() {
    System.out.println("default methods are allowed in interface");
    demo04();
    }
    public static void demo02() {
    System.out.println("static methods are allowed in interface");
    demo05();
    }
    private void demo04() {
    System.out.println("Hey, I am private method");
    }
    private static void demo05(){
    System.out.println("Hey, I am another private method");
    }
}

These private methods do not participate in inheritance. It can be called internally by the default method or static methods within the interface.

Some important key points to take away

  • By default, all the methods are public abstract in interface. So, no need to mention abstract keywords for methods and classes.

  • Interface is a collection of public, abstract methods. We cannot change the access specifier.

  • We cannot create the object of the interface. But, reference variable we can create it.

  • Interface cannot have parent class.

  • Interface can extends multiple interfaces. But it will never implement anything.

  • Interface can also have variables. But it's a good practice if we declared that variable as "public static final".

  • Please note that class can extend only one class and can implement multiple interfaces.

"Thanks for taking time to read this post, I hope you find it useful"