Interfaces are one of the fundamental features of the Java programming language. They allow you to provide a set of methods that users can call to interact with your code — without understanding their implementation. However, to be a proficient Java developer, you need to know how they work.
In this article, you’ll learn what Java interfaces are, what they’re used for, and how to use them. To understand how interfaces work, you’ll need to know what a class is and how inheritance works, so read up on those first if necessary.
Data Abstraction in Java
In Java, data abstraction is a way of concealing specific details of a class, showing the user only what they need to know.
For example, as a computer user, you know that if you press a key on your keyboard, the character associated with that key will be printed on your monitor. However, you do not know what’s happening under the hood — that’s an abstraction.
One way to achieve data abstraction in Java is using abstract classes and methods. To make a class or method abstract, you simply define it with the abstract keyword:
The Person class above is an abstract class, so it cannot be used to create an object. Trying to do so will generate an error:
To access an abstract class, it must be inherited from another class called the subclass. This subclass must then implement the body of every abstract method declared inside the abstract class. See the example below:
Java interfaces take this a step further by applying abstraction to the entire class, not just specific methods. Every method in an interface is declared without a body, and every field is public, final, and static by default.
What is an interface in Java?
In Java, an interface is similar to an abstract class in most ways. As with plain abstract classes, you can't create an object from an interface.
With an interface, you have total class abstraction — no method declared in an interface can have a body. Instead, the body of all the methods must be implemented in the class that implements the interface. For example:
Here, the Person interface has one public field and two interface methods: speak and greet. The TallPerson class then implements the interface and provides the body for its two methods — failure to do so will throw an error. Then, inside the Woman class, you can create an object from the implement class and call any of its methods.
Implementing Multiple Interfaces in Java
In Java, a class can also implement multiple interfaces, allowing you to implement as many of them as you want. In the following example, the Mammal class implements the four interfaces declared on top of it:
Once again, the methods declared in each of the implemented interfaces must be overridden in the implement class. If you fail to do so, you’ll get an error telling you to implement the inherited abstract method in the implement class.
Making an Interface to Extend Multiple Interfaces in Java
An interface in Java can also extend across multiple interfaces. This is useful for grouping related interfaces. To make an interface extend one or more interfaces, you simply use the extends keyword. Here’s an example:
The Human interface extends Cat, Dog, and Bat. As a result, the Mammal class only needs to implement Human, which can access the other three interfaces.
Features Added to Interfaces in JDK 8
If you’re using JDK 8 or higher, you can specify the default implementation for interface methods — interfaces can now have method bodies! You can do that using default and static methods in interfaces.
In the following example, inside Test, we implement a body for the static method show. As a result, the class that implements Test can simply use the default show method — or override it if needed:
Additionally, you can now define static methods in an interface. These can be called independently without first creating an object. For example:
Understanding Interfaces in Java
In Java, an interface is used to implement full abstraction in classes. This means that all fields are static, public, and final by default. It also means that all methods in an interface are unable to have a body — the body must be implemented in the class that implements the interface. The exception lies in Java 8, where you can implement the bodies of the functions in interfaces.
A class can implement multiple interfaces. When this happens, the implement class must include the body implementation of all methods declared in the interfaces.
Finally, an interface can also extend one or more other interfaces. When this happens, the class simply needs to implement one of the interfaces (the one that extends the others) to access the others.