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.
What You'll Learn in This Lesson
📚 Before You Start
This lesson builds on classes and inheritance. Make sure you're comfortable with:
- Classes & Objects — defining a class and creating objects from it
- Inheritance — extending a class with extends and overriding methods
💡 The Big Idea — A Job Description
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.
1️⃣ Declaring and Implementing an Interface
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.
🎯 Your Turn #1 — Implement a Shape
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.
2️⃣ Multiple Interfaces
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.
3️⃣ Default, Static Methods, and Constants
Since Java 8 an interface can ship real behaviour, not just signatures:
- default method — has a body. Every implementing class inherits it for free, but can override it. Use it to add a method to an interface without breaking existing implementers.
- static method — belongs to the interface itself. You call it on the interface name, e.g. Flyable.info() , never on an instance.
- Constants — any field you declare is automatically public static final . So int MAX_ALTITUDE = 10000; is a shared, unchangeable value.
🎯 Your Turn #2 — A Document That Prints and Saves
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.
4️⃣ Interface Inheritance & Functional Interfaces
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.
5️⃣ Interface vs Abstract Class
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.
Common Errors (and the Fix)
- ❌ Forgetting to implement every method: Car is not abstract and does not override abstract method getSpeed() in Drivable . Fix: provide a body for every method in the interface (and any interfaces it extends), or declare the class abstract too.
- ❌ Weaker access on an implementation: attempting to assign weaker access privileges; was public . Interface methods are public , so your implementations must be public — leaving the keyword off makes them package-private and won't compile. Fix: add public .
- ❌ The diamond problem with default methods: implement two interfaces that both supply a default method of the same name and you get class inherits unrelated defaults . Fix: override the method in your class and, if you want one parent's version, call InterfaceName.super.methodName() .
- ❌ Trying to instantiate an interface: Drivable d = new Drivable(); gives Drivable is abstract; cannot be instantiated . An interface has no bodies to run. Fix: create an implementing class — Drivable d = new Car(); — or use an anonymous class / lambda.
📋 Quick Reference — Interface vs Abstract Class
Feature
Interface
Abstract Class
Keyword to use it
implements
extends
How many at once
Many ( implements A, B )
One only
Methods
Abstract + default + static
Abstract + concrete
Fields
Constants only ( public static final )
Any fields, incl. mutable state
Constructor
❌ None
✅ Yes
Can be instantiated?
❌ No
Best for
Unrelated classes sharing a capability
Related classes sharing code + state
❓ Frequently Asked Questions
🏆 Mini-Challenge — Build a Notifier
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.
🎉 Lesson Complete!
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.
Continue this course
- Previous: Previous Lesson
- Next: Next Lesson