Java Reference Library →

interface Keyword Usage and Examples

interface Keyword Usage and Examples

Learn how to use the Java interface keyword with clear examples. Understand how interfaces work, when to use them, and how they support abstraction and polymorphism.

This reference explains interface Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

interface Keyword in Java

In Java, the interface keyword is used to declare an interface. An interface is a blueprint of a class that contains only abstract methods (until Java 7) and static/final variables. Interfaces help achieve abstraction and support multiple inheritance in Java, which is otherwise not possible with classes.

Why Use Interfaces?

Interfaces are useful when:

  • You want to define a contract for what a class can do, without specifying how it does it.
  • You want to achieve loose coupling between components.
  • You need multiple classes to follow the same method structure.

Basic Syntax of an Interface

interface InterfaceName {
    // constant variables
    // abstract methods
}

Example: Defining and Implementing an Interface

interface Animal {
    void makeSound();  // abstract method
}

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Bark!");
    }
}

class Cat implements Animal {
    public void makeSound() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a1 = new Dog();
        Animal a2 = new Cat();

        a1.makeSound();
        a2.makeSound();
    }
}
Bark!
Meow!

Key Rules of Interfaces

  • All methods are implicitly public and abstract (unless marked otherwise in Java 8+).
  • All variables are implicitly public static final.
  • A class uses the implements keyword to implement an interface.
  • A class must implement all methods of the interface, or declare itself as abstract.

Interfaces vs Abstract Classes

AspectInterfaceAbstract Class
Multiple InheritanceSupportedNot supported
ConstructorsNot allowedAllowed
Access ModifiersOnly public methodsAny access modifier
Default BehaviorNo state or constructorsCan have fields and state

Default and Static Methods in Interfaces (Java 8+)

Since Java 8, interfaces can have:

  • default methods — methods with a body.
  • static methods — utility methods tied to the interface.
interface Calculator {
    default void greet() {
        System.out.println("Welcome to Calculator!");
    }

    static void info() {
        System.out.println("Calculator Interface v1.0");
    }

    int add(int a, int b);
}

class BasicCalculator implements Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator c = new BasicCalculator();
        c.greet();
        System.out.println("Sum: " + c.add(5, 3));
        Calculator.info();
    }
}
Welcome to Calculator!
Sum: 8
Calculator Interface v1.0

Functional Interfaces (Java 8+)

A functional interface is an interface with exactly one abstract method. These are the basis for lambda expressions.

@FunctionalInterface
interface Printer {
    void print(String msg);
}

public class Main {
    public static void main(String[] args) {
        Printer p = (msg) -> System.out.println(msg);
        p.print("Lambda says hi!");
    }
}
Lambda says hi!

When to Use Interfaces in Real-World Java Apps

  • To define APIs or SDKs (e.g., JDBC interfaces)
  • For dependency injection and mocking in unit testing
  • In large systems with multiple implementations of a common behavior

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →