Interfaces

An interface is a contract: a list of methods a class promises to provide. Learn to declare and implement them, combine several at once, add default and static methods, and tell an interface from an abstract class.

Learn Interfaces in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

This lesson builds on classes and inheritance. Make sure you're comfortable with:

Analogy: An interface is a job description . It lists what the role must be able to do — "write reports, attend meetings, hit deadlines" — but it never says how . Two different people (classes) can both satisfy the same job description in completely different ways.

In code, the interface lists method names with no bodies. Any class that says implements the interface is signing the contract: it must supply a real version of every listed method. The payoff is that you can write one method that accepts the interface and it works with every class that implements it — present and future. That is what makes interfaces the backbone of flexible, testable Java.

You declare an interface with the interface keyword and list method signatures — a signature is just the return type, the name, and the parameters, with a semicolon instead of a body. These methods are automatically public and abstract , so you never write those words.

A class then implements the interface and provides a body for every method. Because the contract methods are public , your implementations must be public too.

The Square class promises to be a Shape , but two methods are blank. Fill in the ___ blanks so it satisfies the contract. Run it and check it against the expected output in the comment.

A class can only extends one parent class, but it can implements as many interfaces as you like — just separate them with commas. This is how Java gives you the flexibility of multiple inheritance without its headaches.

A Duck now counts as a Flyable and a Swimmable . You can pass it to any method expecting either type. It just has to provide every method from every interface it signs up for.

Since Java 8 an interface can ship real behaviour, not just signatures:

A Document should be both Printable and Saveable . Fill the blanks so it implements both interfaces and provides the missing method. Notice you get preview() for free from the default method.

Interfaces can extends other interfaces to build bigger contracts. A class that implements the child must satisfy the whole chain — every method, inherited or added.

When an interface has exactly one abstract method, it's a functional interface , and you can implement it with a one-line lambda instead of a whole class. Mark it with @FunctionalInterface and the compiler checks there's only one abstract method.

Both let you define methods a subclass must implement, so when do you reach for which? An abstract class is a partial class: it can hold normal fields, a constructor, and finished methods, but a class can extends only one. An interface is a pure contract a class can implement many of.

Analogy: an abstract class is a half-built house — the foundations and framing are already poured (shared code and state), you just finish the rooms. An interface is the building code — a list of requirements any house must meet, but it builds nothing itself.

Time to write it from scratch. The starter below is just an outline of comments — design the interface and two implementing classes yourself, then loop over them polymorphically. Check your result against the expected output.

Great work! You can now declare an interface, implement it (even several at once), add default and static methods and constants, use a functional interface with a lambda, build contracts through interface inheritance, and pick an interface or an abstract class with confidence. These are the tools every Java framework — Spring, Android, Jakarta — is built on.

Next up: Exception Handling — catch and recover from errors with try-catch so your program never crashes unexpectedly.

Practice quiz

Which keyword does a class use to provide an interface's methods?

  • extends
  • inherits
  • implements
  • uses

Answer: implements. A class uses 'implements' to fulfil an interface contract.

By default, interface methods (without a body) are implicitly...

  • public and abstract
  • private and static
  • protected and final
  • package-private

Answer: public and abstract. Interface abstract methods are implicitly public and abstract.

How many interfaces can a single class implement?

  • Exactly one
  • At most two
  • None
  • As many as you like

Answer: As many as you like. A class can implement many interfaces, separated by commas.

What kind of interface method has a body and is inherited for free by implementers?

  • abstract
  • default
  • private
  • final

Answer: default. A 'default' method (Java 8+) provides a body every implementer inherits.

A field declared in an interface like 'int MAX = 10000;' is implicitly...

  • public static final
  • private final
  • protected static
  • volatile

Answer: public static final. Interface fields are implicitly public static final — constants.

How do you call a static interface method named info() on interface Flyable?

  • new Flyable().info()
  • this.info()
  • Flyable.info()
  • super.info()

Answer: Flyable.info(). Static interface methods are called on the interface name: Flyable.info().

What is a functional interface?

  • An interface with no methods
  • An interface with exactly one abstract method
  • An interface that extends a class
  • An interface with only static methods

Answer: An interface with exactly one abstract method. A functional interface has exactly one abstract method, so a lambda can implement it.

Why must a class's implementations of interface methods be declared public?

  • For speed
  • Because of generics
  • To enable inheritance
  • Interface methods are public, and you cannot assign weaker access

Answer: Interface methods are public, and you cannot assign weaker access. You cannot give weaker access than the interface's public methods, so implementations must be public.

Can an interface extend another interface?

  • No, never
  • Yes, even several at once
  • Only abstract classes
  • Only if it is functional

Answer: Yes, even several at once. Interfaces can extend other interfaces to build larger contracts.

When should you prefer an abstract class over an interface?

  • Always
  • When you need multiple inheritance
  • When you need shared mutable state (fields) or a constructor
  • When you only have constants

Answer: When you need shared mutable state (fields) or a constructor. Start with an interface; switch to an abstract class when you need shared state or a constructor.