Java Interfaces: How to Use Them

Roger Winter

Published:

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.

person using java interfaces in the java programming language

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.

Download Now: An Introduction to Java & JavaScript

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:

abstract class Person { public abstract void speak(); public void greet() { System.out.println("Hello"); } }

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:

Person John = new Person(); // this throws 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:

// TallPerson inherits from Person and implements its sole abstract method class TallPerson extends Person { public void speak() { // The body of speak() goes here System.out.println("It's me, a tall person!"); } public static void main(String[] args) { TallPerson sarah = new TallPerson(); // Accessing Person class through TallPerson sarah.speak(); sarah.greet(); } }

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:

// Interface interface Person { // public, static, and final final int species = "Homo sapiens"; // interface methods (does not have a body) public void speak(); public void greet(); } // TallPerson "implements" the Person interface class TallPerson implements Person { public void speak() { // The body of speak() goes here System.out.println("It's me, a tall person!"); } public void greet() { // The body of greet() goes here System.out.println("Hello"); } } class Woman { public static void main(String[] args) { TallPerson sarah = new TallPerson(); // Create a TallPerson object sarah.speak(); sarah.greet(); } }

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:

interface Human{ public void greet(); } interface Dog{ public void bark(); } interface Bat{ public void click(); } interface Cat{ public void meow(); } public class Mammal implements Human,Dog,Bat,Cat { public static void main(String[] args){ Mammal animalGroup = new Mammal(); animalGroup.greet(); animalGroup.bark(); animalGroup.click(); animalGroup.meow(); } public void greet() { System.out.println("greeting"); } public void bark() { System.out.println("barking"); } public void click() { System.out.println("making click sounds"); } public void meow() { System.out.println("meowing"); } }

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:

interface Dog{ public void bark(); } interface Bat{ public void click(); } interface Cat { public void meow(); } interface Human extends Dog, Bat, Cat{ public void greet(); } public class Mammal implements Human { public static void main(String[] args){ Mammal animalGroup = new Mammal(); animalGroup.greet(); animalGroup.bark(); animalGroup.click(); animalGroup.meow(); } public void greet() { System.out.println("greeting"); } public void bark() { System.out.println("barking"); } public void click() { System.out.println("making click sounds"); } public void meow() { System.out.println("meowing"); } }

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:

interface Test { final int name = "test interface"; default void show() { System.out.println("showing test interface"); } } // This class implements the interface. class Main implements Test { public static void main (String[] args) { Main tt = new Main(); tt.show(); } }

Additionally, you can now define static methods in an interface. These can be called independently without first creating an object. For example:

interface Test { final int name = "test interface"; static void show() { System.out.println("showing test interface"); } } // This class implements the interface. class Main implements Test { public static void main (String[] args) { Test.show(); } }

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.

java

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about one of the world's most popular programming languages.