Menu

Java Interfaces: Defining Contracts and implements

What a Java interface is, how to define and implement one, default and static methods, and how interfaces differ from abstract classes.

This page includes runnable editors - edit, run, and see output instantly.

An Interface Is a Contract

An interface declares what a class can do without saying how. It is a list of method signatures that any implementing class promises to fulfill. The point is decoupling: code can work with the interface type and not care which concrete class is behind it.

Circle implements Shape means Circle must define area and perimeter. Once it does, a Circle is a Shape and can be stored in a Shape variable.

Many Classes, One Type

The power shows when several unrelated classes implement the same interface. Code written against Shape handles all of them:

The loop never asks "is this a Circle or a Rectangle?" - it just calls area() and trusts the contract. Adding a Triangle later needs no change here.

Implementing Multiple Interfaces

A class extends only one class, but it can implement any number of interfaces - this is how Java does "multiple inheritance" of behavior safely:

List the interfaces comma-separated after implements. The class must satisfy all of them.

default and static Methods

Since Java 8, an interface can ship method bodies. A default method gives implementing classes a ready-made implementation they inherit for free (and may override):

default methods let an interface evolve - adding a method without breaking every existing implementer. A static interface method, by contrast, behaves like other static members: it is a utility you call on the interface itself, like Comparator.naturalOrder().

Constants

Fields in an interface are implicitly public static final - they are constants, not instance state:

interface Physics {
    double GRAVITY = 9.81;   // automatically public static final
}

Interfaces traditionally hold no per-object state; the absence of instance fields is part of what distinguishes them from classes.

Functional Interfaces

An interface with exactly one abstract method is a functional interface, and you can implement it with a lambda expression instead of a whole class:

This is the foundation of Java's lambdas and the java.util.function types (Function, Predicate, Supplier, and friends). The @FunctionalInterface annotation makes the intent explicit and lets the compiler enforce the single-method rule.

Interface vs Abstract Class

Both let you program to an abstraction, so when do you pick which?

  • Interface - a capability that unrelated classes can share. A Bird and an Airplane can both be Flyable. A class can implement many. No instance state.
  • Abstract class - shared state and code among closely related classes. A Cat and a Dog both extend Animal, inheriting common fields and partially implemented methods. A class extends only one.

A common pattern combines them: an interface defines the contract, and an abstract class implements the boilerplate parts so concrete subclasses fill in only the specifics.

Next: Abstract Classes

Interfaces define behavior with no state. Abstract classes sit in between interfaces and full classes - they can declare unimplemented methods and carry fields and constructors. That is the next page.

Frequently Asked Questions

What is an interface in Java?

An interface is a contract: a set of method signatures (and constants) that a class promises to provide. It says what a class can do, not how. A class uses the implements keyword to take on an interface and must supply a body for every abstract method the interface declares. Interfaces let unrelated classes be used interchangeably through a shared type.

What is the difference between an interface and an abstract class in Java?

A class can implement many interfaces but extend only one (abstract) class. Interfaces traditionally hold no instance state and only declare behavior, while an abstract class can have fields, constructors, and partially implemented logic. Use an interface to define a capability several unrelated classes can share; use an abstract class to share common state and code among closely related subclasses.

Can a Java interface have method bodies?

Yes, since Java 8. Interface methods marked default provide a body that implementing classes inherit (and may override), and static interface methods provide utility behavior callable on the interface itself. Regular interface methods are still abstract - just a signature - and implementing classes must define them.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED