Interfaces in Java: Definition, implements and Examples
An interface in Java is a reference type that defines a contract: which methods a class must provide, without saying how they work. A class adopts an interface with the implements keyword and overrides all of its abstract methods. Because one class can implement several interfaces, interfaces are the foundation of polymorphism and loosely coupled code in Java.
Classically, a Java interface contains abstract public methods (public abstract) and static constants (public static final). Since Java 8 an interface can also have default methods (default) and static methods, and since Java 9 — private methods.
What is an interface in Java
An interface lets you specify what an implementing class must do, but not how it does it — the class chooses the implementation. An interface has no instance fields, so it does not hold object state: any variable declared in an interface is always a public static final constant. Interfaces are one of the main mechanisms of polymorphism in Java: one interface, many implementations.
Look at the picture below. We have a contract (the interface) that describes which actions a mouse must support — for example, a right click and a left click. Different mouse manufacturers (classes) that implement this contract must build mice that support these actions. What the mice look like and which extra features they have is up to each manufacturer.

Like classes, interfaces can be declared public or package-private (no modifier — the interface is then visible only inside its own package). See the lesson on access modifiers in Java for details.
How to create an interface in Java: step by step
- Create a file named after the interface, for example
Moveable.java. Just like a public class, apublicinterface must live in a file with the same name. - Declare it with the
interfacekeyword instead ofclass:public interface Moveable { ... }. - Declare methods without a body — just the signature followed by a semicolon. Add constants if you need them.
- Create a class that implements the interface with
implementsand override all of its abstract methods (see the next section).
Interface variables are public static final by default, and you do not have to write these modifiers. In the example below, RIGHT, LEFT, UP and DOWN are declared without any modifiers, yet they are still public static final.
Abstract interface methods are implicitly public abstract, and those modifiers are optional too. Such methods have no body; each declaration ends with a semicolon:
public interface Moveable {
int RIGHT = 1;
int LEFT = 2;
int UP = 3;
int DOWN = 4;
void moveRight();
void moveLeft();
} Tip
Constants in an interface are allowed, but an interface that exists only to hold constants is a well-known anti-pattern (the Constant Interface). For a set of related values such as movement directions, modern code uses an enum instead.
Implementing an interface: the implements keyword
To state that a class implements an interface, add the implements keyword and the interface name to the class declaration. The rules are:
- the class must override every abstract method of the interface (including those inherited from its parent interfaces);
- the implementing method must be
public— interface methods are public, and overriding can never narrow access; - the
@Overrideannotation is optional but useful: the compiler checks that the signature matches the interface; defaultmethods do not have to be overridden — the class inherits their implementation from the interface;- if not all abstract methods are implemented, the class must be declared abstract.
Besides the interface methods, a class can declare methods of its own. For example, the Transport class below implements Moveable: it provides moveRight() and moveLeft() and adds its own stop() and start() methods:
public class Transport implements Moveable {
public void moveRight() {
System.out.println("The vehicle turns right.");
}
public void moveLeft() {
System.out.println("The vehicle turns left.");
}
public void stop() {
System.out.println("The vehicle stops.");
}
public void start() {
System.out.println("The vehicle starts.");
}
} Any number of classes can implement the same interface. In the diagram below, two more classes — Robot and Device — also implement Moveable.

The Robot class from the diagram:
public class Robot implements Moveable {
public void moveRight() {
System.out.println("The robot turns right.");
}
public void moveLeft() {
System.out.println("The robot turns left.");
}
} If a class implements an interface but not all of its abstract methods, it must be declared abstract. The Device class implements only one method of Moveable, so it is abstract. The remaining moveLeft() method must be implemented by the first non-abstract subclass of Device:
public abstract class Device implements Moveable {
public void moveRight() {
System.out.println("The device turns right.");
}
} A class can extend another class and implement interfaces at the same time. The order in the declaration is fixed: extends first, then implements:
public class ElectricCar extends Car implements Moveable, Chargeable {
// implementations of the Moveable and Chargeable methods
} An interface as a variable type
The variable is declared as Moveable moveable = new Transport();, the Transport class has a start() method — and yet moveable.start(); does not compile. The compiler looks at the type of the variable, not at the actual object, and only allows calls that the interface promises.
You can use an interface type to declare variables that refer to objects of any class implementing that interface. What you cannot do is create an object of the interface itself: Moveable moveable1 = new Moveable(); is a compile-time error. The start() method is available through the transport variable of type Transport, but not through moveable:
public class TransportExample {
public static void main(String[] args) {
Moveable moveable = new Transport();
Transport transport = new Transport();
Moveable robot = new Robot();
// Moveable moveable1 = new Moveable(); // compile-time error
// moveable.start(); // compile-time error
moveable.moveRight();
moveable.moveLeft();
System.out.println();
transport.start();
transport.moveRight();
transport.moveLeft();
transport.stop();
System.out.println();
robot.moveLeft();
robot.moveRight();
}
} Output:
The vehicle turns right.
The vehicle turns left.
The vehicle starts.
The vehicle turns right.
The vehicle turns left.
The vehicle stops.
The robot turns left.
The robot turns right. Implementing multiple interfaces
A class can implement any number of interfaces. This is how Java handles multiple inheritance of type: with inheritance a class extends only one class, but it can implement as many interfaces as it needs. The diagram below shows a Pickup class that implements two interfaces — CargoAuto and PassengersAuto:

public interface CargoAuto {
void transportCargo();
} public interface PassengersAuto {
void transportPassengers();
} To implement several interfaces, list them after implements, separated by commas. The Pickup class must define all methods of every interface it implements:
public class Pickup implements CargoAuto, PassengersAuto {
public void transportCargo() {
System.out.println("Carrying cargo");
}
public void transportPassengers() {
System.out.println("Carrying passengers");
}
} Nested interfaces
An interface declared inside a class or another interface is called a nested (or inner) interface. A nested interface is always implicitly static. Here, NestedIf is declared inside class A:
public class A {
public interface NestedIf {
boolean isNotNegative(int x);
}
} From outside, you refer to it through the enclosing class — A.NestedIf:
public class B implements A.NestedIf {
public boolean isNotNegative(int x) {
return x >= 0;
}
} public class NestedIfExample {
public static void main(String[] args) {
A.NestedIf nif = new B();
if (nif.isNotNegative(10)) {
System.out.println("10 is not negative.");
}
if (nif.isNotNegative(-12)) {
System.out.println("This will not be printed.");
}
}
} The best-known nested interface in the standard library is Map.Entry — a key–value pair declared inside the Map interface.
Extending interfaces
An interface can inherit from another interface using the extends keyword. Unlike a class, an interface can extend several interfaces at once.
In the diagram below, two different interfaces — Hockey and Football — extend the base interface Sport. Any number of interfaces can extend the same parent, each adding its own methods. Football also extends two interfaces at the same time — Sport and TVProgram.

public interface Sport {
void setHomeTeam(String name);
void setVisitingTeam(String name);
} public interface Hockey extends Sport {
void homeGoalScored();
void visitingGoalScored();
void endOfPeriod(int period);
void overtimePeriod(int ot);
} public interface TVProgram {
void switchToChannel();
} public interface Football extends Sport, TVProgram {
void homeTeamScored(int points);
void visitingTeamScored(int points);
void endOfQuarter(int quarter);
} A FootballImpl class that implements Football must override the methods of all three interfaces: Football, TVProgram and Sport. It does not implement the Hockey methods — that is a different branch of the hierarchy; a class implementing Hockey would implement the methods of Hockey and Sport:
public class FootballImpl implements Football {
@Override
public void setHomeTeam(String name) {
System.out.println("Setting home team");
}
@Override
public void setVisitingTeam(String name) {
System.out.println("Setting visiting team");
}
@Override
public void switchToChannel() {
System.out.println("Switching to channel");
}
@Override
public void homeTeamScored(int points) {
System.out.println("Home team scored");
}
@Override
public void visitingTeamScored(int points) {
System.out.println("Visiting team scored");
}
@Override
public void endOfQuarter(int quarter) {
System.out.println("End of quarter");
}
} Since Java 17 you can restrict who may implement an interface: sealed interface Shape permits Circle, Square allows only the listed classes to implement Shape.
Default methods in interfaces
Java 8 added default methods to interfaces — methods that have an implementation right inside the interface. In the Java 8 specification work (JSR 335) they were originally called virtual extension methods. Implementing classes do not have to override them, but may do so when needed. Default methods are declared with the default keyword.
The main reason they exist is API evolution: default methods made it possible to add stream() and forEach() to Collection without breaking millions of existing implementations.
The SomeInterface interface declares a default method defaultMethod() with a basic implementation:
public interface SomeInterface {
default String defaultMethod() {
return "Default String value";
}
} The SomeInterfaceImpl1 class implements this interface and does not override defaultMethod() — that is perfectly legal:
public class SomeInterfaceImpl1 implements SomeInterface {
} If the default implementation does not suit SomeInterfaceImpl2, the class overrides the method:
public class SomeInterfaceImpl2 implements SomeInterface {
@Override
public String defaultMethod() {
return "A different string";
}
} Now we create objects of SomeInterfaceImpl1 and SomeInterfaceImpl2 and call defaultMethod() on each. For SomeInterfaceImpl1 the interface implementation runs; for SomeInterfaceImpl2 — its own override:
public class DefaultMethodExample {
public static void main(String[] args) {
SomeInterface obj1 = new SomeInterfaceImpl1();
SomeInterface obj2 = new SomeInterfaceImpl2();
System.out.println(obj1.defaultMethod());
System.out.println(obj2.defaultMethod());
}
} Output:
Default String value
A different string Default method conflicts
If a class implements two interfaces that each have a default method with the same signature, the compiler cannot choose which implementation to use and reports an error. The class must override the method itself. To call a specific interface's implementation, use the InterfaceName.super.method() syntax:
interface Camera {
default String info() {
return "Camera";
}
}
interface Phone {
default String info() {
return "Phone";
}
}
public class Smartphone implements Camera, Phone {
@Override
public String info() {
return Phone.super.info() + " with a feature: " + Camera.super.info();
}
} The resolution rules are: a class method (including one inherited from a superclass) always wins over an interface default method; a more specific interface (the child) wins over its parent; in every other case the programmer must resolve the conflict explicitly.
Static methods in interfaces
Java 8 also made it possible to declare static methods in interfaces. Static interface methods, like static class methods, are called without an object: you write the interface name, a dot and the method name. They usually hold helper and factory methods, such as List.of() or Comparator.naturalOrder().
public interface MyIf {
int getNumber();
static int staticMethod() {
return 0;
}
} public class MyIfImp implements MyIf {
@Override
public int getNumber() {
return 42;
}
} public class StaticMethodExample {
public static void main(String[] args) {
MyIf obj1 = new MyIfImp();
System.out.println(obj1.getNumber());
System.out.println(MyIf.staticMethod());
// obj1.staticMethod(); // compile-time error
// MyIfImp.staticMethod(); // compile-time error
}
} Output:
42
0 Important
Unlike static methods of a class, static interface methods are not inherited: you cannot call them through an object or through the name of an implementing class. There is only one correct call — MyIf.staticMethod().
Private methods in interfaces
Since Java 9 an interface can declare private methods (both instance and static). They are visible only inside the interface and let you extract code shared by several default or static methods without making it part of the public contract:
public interface Logger {
default void info(String msg) {
log("INFO", msg);
}
default void error(String msg) {
log("ERROR", msg);
}
private void log(String level, String msg) {
System.out.println("[" + level + "] " + msg);
}
} Functional and marker interfaces
Functional interface
A functional interface is an interface with exactly one abstract method (default and static methods do not count). Instead of writing a separate class, you can implement it with a lambda expression or a method reference. The @FunctionalInterface annotation is optional, but it makes the compiler verify that there really is only one abstract method:
@FunctionalInterface
public interface Calculator {
int apply(int a, int b);
}
Calculator sum = (a, b) -> a + b;
System.out.println(sum.apply(2, 3)); // 5 The JDK already ships ready-made functional interfaces — Runnable, Comparator, Function, Predicate, Supplier. They are covered in detail in the lessons "Functional Interfaces" and "The java.util.function Package".
Marker interfaces
A marker interface is an interface that declares no methods and no constants. By implementing it, a class "tags" itself, and the JVM or a library checks the tag with instanceof and changes its behaviour. The classic examples are Serializable (objects of the class can be serialized) and Cloneable.
If a class implements Cloneable, Object.clone() creates a copy of its objects instead of throwing CloneNotSupportedException. Cloneable itself does not declare a clone() method — you still have to override it in the class.
Do not confuse interface with @interface: public @interface Audit {} declares not a regular interface but an annotation, which you place above a class or method (@Audit) rather than implement with implements.
Modern approach
New code usually relies on annotations instead of custom marker interfaces (for example, @Entity or @FunctionalInterface). A marker interface still makes sense when the tag must be part of the type and checked by the compiler.
What an interface can contain: summary table
| Member | Modifiers (implicit) | Has a body | Since Java |
|---|---|---|---|
| Constant | public static final | — (a value is required) | 1.0 |
| Abstract method | public abstract | No | 1.0 |
| Nested class / interface | public static | Yes | 1.1 |
| Default method | public + default | Yes | 8 |
| Static method | public static (by default) | Yes | 8 |
| Private method | private or private static | Yes | 9 |
| Instance field, constructor | — | — | Not allowed |
Mistakes that trip up beginners
- Implementing a method without
public. Abstract interface methods are alwayspublic, and an override cannot narrow access. Writingvoid moveRight() {}in the class without a modifier fails with "attempting to assign weaker access privileges". - Wrong order in the class declaration.
class ElectricCar implements Moveable extends Carwill not compile:extendsalways comes beforeimplements. - Trying to change an interface constant.
Moveable.RIGHT = 5;will not compile: the variable is implicitlyfinal. - Calling an implementation method through an interface-typed variable.
moveable.start()is not available even though the object is aTransport. You need a variable of typeTransportor a cast. - Calling a static interface method through an object or an implementing class — a compile-time error; call it through the interface name.
- Two interfaces with the same default method and no override in the class — a compile-time error.
- Expecting
Cloneableto provideclone(). The interface is empty; you have to override the method yourself.
Frequently asked questions
What does implements mean in Java, and how is it different from extends?
The implements keyword means that a class implements an interface and commits to overriding all of its abstract methods. extends is used for inheritance: a class extends one class, and an interface extends one or more interfaces. A class can combine both keywords: class A extends B implements C, D.
What is the difference between an interface and an abstract class in Java?
A class can extend only one abstract class but implement any number of interfaces. An abstract class can have instance fields, constructors and methods with any access modifier, while an interface holds only constants and has no constructors. Choose an abstract class for shared state and code among related classes, and an interface to describe a capability (a contract) that unrelated classes can have.
Can you create an object of an interface?
Not directly: new Moveable() is a compile-time error. You can, however, create an anonymous class that implements the interface (new Moveable() { ... }), and for a functional interface — a lambda expression. In both cases the object belongs to an implementing class, not to the interface itself.
Can an interface have a constructor?
No. An interface has no instance fields to initialize and cannot be instantiated with new, so a constructor is neither needed nor allowed. Interface constants are initialized where they are declared.
Does Java support multiple inheritance through interfaces?
Yes, multiple inheritance of type: a class can implement several interfaces, and an interface can extend several interfaces. There is no multiple inheritance of state, because interfaces have no instance fields. If the same default method comes from different interfaces, the class must override it.
Comments