Interface in Java – Contracts, Default Methods and Multiple Inheritance
An interface in Java is a reference type that defines a contract: a set of methods that implementing classes promise to provide. A class implements an interface and supplies the method bodies; code that uses the interface type works with every implementation.
Interfaces are the main tool for abstraction and for multiple inheritance of type in Java. Since Java 8 they can also contain default and static methods, and since Java 9 private methods. This tutorial covers all of it with runnable examples.
1. Declaring and Implementing an Interface
[public] interface InterfaceName [extends Interface1, Interface2] {
// constants (public static final)
// abstract methods (public abstract)
// default methods (Java 8)
// static methods (Java 8)
// private methods (Java 9)
}
class ClassName [extends Parent] implements InterfaceName, OtherInterface { ... }
public class InterfaceBasics {
interface Shape {
double PI_APPROX = 3.14; // implicitly public static final
double area(); // implicitly public abstract
String name();
}
static class Square implements Shape {
private final double side;
Square(double side) { this.side = side; }
public double area() { return side * side; } // must be public
public String name() { return "Square"; }
}
static class Circle implements Shape {
private final double r;
Circle(double r) { this.r = r; }
public double area() { return PI_APPROX * r * r; }
public String name() { return "Circle"; }
}
public static void main(String[] args) {
Shape[] shapes = { new Square(3), new Circle(2) }; // interface as a type
for (Shape s : shapes) System.out.println(s.name() + " area = " + s.area());
System.out.println("Constant via interface: " + Shape.PI_APPROX);
}
}
Square area = 9.0 Circle area = 12.56 Constant via interface: 3.14
- Interface methods without a body are implicitly
public abstract. - Fields are implicitly
public static finalconstants. - Implementing methods must be
public, because you cannot reduce visibility. - An interface can be used as a variable, parameter, array or return type.
2. What an Interface Can Contain
| Member | Since | Implicit modifiers / notes |
|---|---|---|
| Constants | 1.0 | public static final; must be initialised |
| Abstract methods | 1.0 | public abstract |
| Default methods | 8 | public; have a body; inherited and overridable |
| Static methods | 8 | public by default; called as Interface.method(); not inherited |
| Private methods | 9 | Helpers for default/static methods; not visible outside |
| Nested types | 1.1 | Implicitly public static |
An interface cannot have instance fields, constructors or initializer blocks, and cannot be instantiated.
3. Multiple Inheritance with Interfaces
A class can extend only one class but implement any number of interfaces, so an object can have several types at once:
public class MultipleInterfaces {
interface Flyable { void fly(); }
interface Swimmable { void swim(); }
static class Animal {
final String name;
Animal(String name) { this.name = name; }
}
// One superclass, many interfaces
static class Duck extends Animal implements Flyable, Swimmable {
Duck() { super("Duck"); }
public void fly() { System.out.println(name + " flies over the lake"); }
public void swim() { System.out.println(name + " paddles on the water"); }
}
static void takeOff(Flyable f) { f.fly(); }
static void dive(Swimmable s) { s.swim(); }
public static void main(String[] args) {
Duck d = new Duck();
takeOff(d); // a Duck is a Flyable
dive(d); // ... and a Swimmable
System.out.println("instanceof Flyable? " + (d instanceof Flyable));
}
}
Duck flies over the lake Duck paddles on the water instanceof Flyable? true
This avoids the "diamond problem" of multiple class inheritance, because interfaces do not carry instance state. If two interfaces supply conflicting default methods, the class must override the method and can choose one with InterfaceName.super.method(); see Default Methods.
4. Default, Static and Private Methods
Modern interfaces can provide behaviour, not just declarations. This discount API uses all three kinds of method bodies:
import java.util.List;
public class ModernInterface {
interface Discount {
double apply(double price); // abstract
default Discount then(Discount next) { // default method (Java 8)
return price -> next.apply(apply(price));
}
static Discount percent(double p) { // static method (Java 8)
check(p);
return price -> price * (1 - p / 100);
}
static Discount flat(double amount) {
return price -> Math.max(0, price - amount);
}
private static void check(double p) { // private method (Java 9)
if (p < 0 || p > 100) throw new IllegalArgumentException("percent out of range: " + p);
}
}
public static void main(String[] args) {
Discount festive = Discount.percent(10).then(Discount.flat(200));
System.out.println("Price after discounts: " + festive.apply(5_000));
List<Discount> options = List.of(Discount.percent(25), Discount.flat(1_000), festive);
double best = options.stream().mapToDouble(d -> d.apply(5_000)).min().orElseThrow();
System.out.println("Best price: " + best);
}
}
Price after discounts: 4300.0 Best price: 3750.0
- Default methods add behaviour to every implementation, which lets libraries evolve interfaces (as
List.sortandMap.forEachwere added in Java 8) without breaking existing classes. - Static methods are factories and utilities that belong to the interface.
- Private methods share code between default and static methods without exposing it.
Discounthas a single abstract method, so it is also a functional interface and can be implemented with lambdas.
5. Interfaces Extending Interfaces
An interface can extend one or more interfaces, combining their contracts:
public class InterfaceExtends {
interface Readable { String read(); }
interface Writable { void write(String data); }
interface ReadWrite extends Readable, Writable { } // an interface can extend many
static class MemoryFile implements ReadWrite {
private final StringBuilder content = new StringBuilder();
public String read() { return content.toString(); }
public void write(String data) { content.append(data); }
}
public static void main(String[] args) {
ReadWrite f = new MemoryFile();
f.write("Hello, ");
f.write("interfaces!");
Readable r = f; // view it through a narrower interface
System.out.println(r.read());
}
}
Hello, interfaces!
6. Special Kinds of Interfaces
- Functional interface: exactly one abstract method; the target type of lambdas (
Runnable,Comparator,Function). - Marker interface: no methods; marks a capability (
Serializable,Cloneable). - Nested interface: declared inside a class or interface (
Map.Entry). - Sealed interface (Java 17):
sealed interface Shape permits Circle, Square {}limits which classes may implement it, enabling exhaustiveswitchpattern matching.
7. Interface vs Abstract Class
| Aspect | Interface | Abstract class |
|---|---|---|
| Keyword to use it | implements | extends |
| How many per class | Many | One |
| Instance fields | No (only constants) | Yes |
| Constructors | No | Yes |
| Method visibility | public (or private helpers) | Any |
| Typical role | Capability or API contract | Shared base implementation |
See Abstract Class vs Interface for a detailed comparison.
8. Best Practices
- Program to interfaces: declare
List<String> names = new ArrayList<>();rather thanArrayList<String>. - Keep interfaces small and focused (interface segregation principle).
- Name them after a capability or role:
Comparable,PaymentGateway,Repository. - Avoid "constant interfaces" that exist only to hold constants; use a final class or enum instead.
- Use default methods for genuine shared behaviour or API evolution, not to turn interfaces into abstract classes.
9. Summary
- An interface defines a contract that classes implement with
implements. - Abstract methods are public abstract; fields are public static final constants.
- Java 8 added default and static methods; Java 9 added private methods.
- A class can implement many interfaces, and an interface can extend many interfaces.
- Interfaces enable abstraction, polymorphism and loose coupling.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is an interface in Java?
A reference type that declares methods a class must implement. It defines a contract and supports abstraction and multiple inheritance of type.
Q2. Why do we use interfaces?
To define contracts independent of implementation, allow multiple inheritance of type, achieve loose coupling and make code easy to extend and test.
Q3. Can an interface have a constructor?
No. Interfaces have no instance state to initialise and cannot be instantiated.
Q4. Can we create an object of an interface?
Not directly. You create objects of implementing classes, anonymous classes or lambdas (for functional interfaces) and refer to them through the interface type.
Q5. Are interface variables final?
Yes. All interface fields are implicitly public static final and must be initialised where declared.
Q6. Why must implemented interface methods be public?
Interface abstract methods are public, and an overriding method cannot have weaker access, so the implementation must also be public.
Q7. Can a class implement multiple interfaces?
Yes, separated by commas: class Duck implements Flyable, Swimmable.
Q8. Can an interface extend another interface?
Yes, and it can extend several: interface ReadWrite extends Readable, Writable. It cannot extend a class.
Q9. What is a default method?
A method with a body in an interface, declared with default. Implementing classes inherit it and may override it.
Q10. Are static interface methods inherited?
No. They must be called with the interface name, for example Comparator.naturalOrder().
Q11. Why were private methods added to interfaces?
To let default and static methods share helper code without making it part of the public API.
Q12. Can an interface have a main method?
Yes. Since Java 8 interfaces can have static methods, including public static void main.
Q13. What happens if a class does not implement all interface methods?
It must be declared abstract; otherwise compilation fails. Default methods do not need to be implemented.
Q14. Can an interface be final?
No. An interface exists to be implemented, so final would make it useless. Use sealed to restrict implementations.
Q15. Can an interface be private or protected?
A top-level interface can be public or package-private. Nested interfaces inside a class can have any access modifier.
Q16. What is the diamond problem with interfaces?
If a class inherits the same default method from two interfaces, the compiler requires the class to override it. It may call a specific one with A.super.method().
Q17. What is a sealed interface?
An interface (Java 17+) that lists its permitted implementations with permits. The compiler then knows all subtypes, enabling exhaustive pattern matching.
Q18. Is an interface a class?
No, it is a separate kind of reference type. It compiles to a .class file, and Class.isInterface() returns true for it.
Q19. Can interfaces have nested classes?
Yes. Nested classes, interfaces, enums and records in an interface are implicitly public static.
Q20. What does "program to an interface" mean?
Using interface types for variables, parameters and return values so code depends on the contract rather than a specific implementation.