Investigating Interfaces In Java

We sometimes need to define a protocol of behaviour that can be used by any class. For example, a car shop could have a car class defining the car features and manage purchases through an inventory system. But what if they wanted to branch out and sell bikes as well? Obviously, they would need a new class for bikes but instead of just repeating shared information like prices and order numbers, we could use an interface which both classes can implement.

What is an Interface and Why Should I Use Them?

Generally, an interface is defined as a device or system that unrelated things use to interact with each other. For example, your mouse and keyboard are an interface between you and your computer, or your remote control is an interface between you and your TV.

In Java, an interface is similar to a class, but unlike classes, it only contains method signatures (the name and parameter types of a method but not its implementation) and constants. You can think of it as a sort of blueprint for a class - they specify what the class has to do but not how to do it. There are a number of scenarios where you should use an interface:

  1. If a problem involves different class hierarchies and needs multiple inheritances to be solved.
  2. If you expect unrelated classes to implement the interface.
  3. You want to specify the behaviour of a particular data type but aren't concerned about who implements its behaviour.
To define an interface in Java, you use the interface keyword. For example:

public interface Sender {
    void send(File fileToBeSent);
}

Which could then be implemented by a new class using the implement keyword like so:
public class ImageSender implements Sender {
    @Override
    public void send(File fileToBeSent) {
        // image sending implementation code.
    }
}

Note that when we implement the Sender interface, we can use the @Override keyword to replace the empty method signature of send. Since Java 8, interfaces can now have default and static methods as well as abstract methods. 

There are a few key things to note about interfaces:

  • You cannot declare a constructor inside an interface - all data members are public static final by default. If a class implements multiple interfaces, they would have to implement multiple constructors to instantiate an object of that class - this is not possible.
  • You cannot instantiate an interface - without a constructor or any concrete methods, nothing will happen.

  • You cannot override an interface method with visibility that is non-public

Abstraction

Abstraction is an important concept in programming in any Object-Oriented Programming language. It is the process of hiding the implementation details and only showing the functionality to the user. For example, when we send an email, we are just shown where to type the text and send the message but not how the actual sending works. Interfaces are one of two main ways of achieving abstraction in Java. The other is using abstract classes which we will talk about later.

Abstract Methods

An abstract method is a type of method that doesn't require implementation for its declaration meaning that they do not have a body, just the method signature. To declare an abstract method, we use the abstract keyword. For example:

abstract double abstractMethod(double x, double y);

Note here that we do not use curly braces ({}) here but have a semicolon (;) at the end. Any class that contains an abstract method should be declared as an abstract class.

Abstract Classes

An abstract class is a class that contains the abstract keyword. It is a restricted class that cannot be used to create objects and must be accessed using the extends keyword. For example:

abstract class abstractClass{
    public abstract int sumOfTwo(int n1, int n2);
    public void disp(){
        System.out.println("Method of class Sum);
    }
}

Which could be inherited by:

public class Inheritor extends abstractClass{
    public int sumOfTwo(int n1, int n2){
        return n1 + n2
    }   
}

As you can see here, an abstract class doesn't have to only contain abstract methods, it can contain regular methods too. Also, note that you do not need to use the @Override keyword to override the abstract method. However, you must still override any abstract methods that it inherits, or it will result in a compilation error. Regular methods, on the other hand, do not necessarily need to be overridden.

Differences between Interfaces and Abstract Classes

There are several key differences between interfaces and abstract classes.
  • Interfaces can only have final static variables and never change its own state wheras abstract classes can have any type of instance or static variables.
  • A class can implement multiple interfaces but only extend one abstract class.
  • Interfaces can be implemented with the implements keyword whereas abstract classes are extended using the extends keyword.
  • Interfaces cannot have a constructor, but abstract classes can.
  • Interfaces can only have abstract, default and static (Java 8) and private (Java 9) methods whereas abstract classes can have any kind of methods.

References

Comments