1. Introduction
An interface in Java is a reference type, similar to a class, that can contain only abstract methods (before Java 8), constants, default methods, static methods, and private methods (Java 9+). It defines a contract of *what* a class must do, without dictating *how* it does it.
Cricket analogy: A team's playing contract with the ICC states a bowler must deliver six-ball overs and follow the no-ball rule, but never dictates whether he bowls seam or spin, just like an interface.
Interfaces are Java's mechanism for achieving multiple inheritance of type, since a class can implement any number of interfaces even though it can extend only one class.
Cricket analogy: An all-rounder like Ravindra Jadeja can simultaneously fulfill the bowler and batsman roles on the team sheet, even though he belongs to only one franchise, mirroring a class implementing many interfaces but extending one class.
2. Syntax
interface InterfaceName {
// constant field (implicitly public static final)
int MAX_VALUE = 100;
// abstract method (implicitly public abstract)
void doSomething();
// default method (Java 8+)
default void greet() {
System.out.println("Hello from default method");
}
// static method (Java 8+)
static void info() {
System.out.println("Static interface method");
}
}
class MyClass implements InterfaceName {
public void doSomething() {
System.out.println("Doing something");
}
}3. Explanation
Every method declared in an interface without a body is implicitly public and abstract — you never need to (and should not) write those keywords explicitly. Every field declared in an interface is implicitly public static final, meaning it behaves like a compile-time constant and must be initialized at declaration.
Cricket analogy: Just as every player automatically wears the team's public jersey number without needing to announce it, every interface method is silently public and abstract, and every field is silently a fixed constant like the boundary rope distance.
A class uses the implements keyword to signal that it fulfills an interface's contract, and it must override every abstract method of that interface (unless the class itself is abstract). A class can implement multiple interfaces separated by commas: class MyClass implements InterfaceA, InterfaceB. Similarly, one interface can extend multiple other interfaces, unlike classes which can extend only one superclass.
Cricket analogy: A player signing with Chennai Super Kings implements the franchise contract by fulfilling every clause, and one player can sign with both their national board and IPL franchise at once, unlike a club that plays in only one league.
Java 8 introduced default methods (with a body, usable without overriding) and static methods (called on the interface itself) to let interfaces evolve without breaking existing implementations. Java 9 added private interface methods to share code between default methods.
Cricket analogy: Java 8's default methods are like the DRS rule added mid-career that older umpires can use without retraining, and static methods are like ICC's own ball-tampering ruling that applies to the sport itself, not any one team.
Exam tip: All interface fields are implicitly public static final, and all abstract methods are implicitly public abstract. Writing these modifiers explicitly is legal but redundant — examiners often test whether you know they are optional, not required.
If a class implements two interfaces that both declare a default method with the same signature, the class MUST override that method itself to resolve the ambiguity, otherwise it is a compile-time error.
4. Example
interface Swimmer {
void swim();
default void breathe() {
System.out.println("Breathing air");
}
}
interface Runner {
void run();
}
// A class can implement multiple interfaces
class Duck implements Swimmer, Runner {
public void swim() {
System.out.println("Duck is swimming");
}
public void run() {
System.out.println("Duck is running");
}
}
public class Main {
public static void main(String[] args) {
Duck duck = new Duck();
duck.swim();
duck.run();
duck.breathe();
}
}5. Output
Duck is swimming
Duck is running
Breathing air6. Key Takeaways
- Interface methods without a body are implicitly public abstract.
- Interface fields are implicitly public static final (constants).
- A class can implement multiple interfaces, achieving multiple inheritance of type.
- An interface can extend multiple other interfaces.
- Java 8+ allows default and static methods with a body inside interfaces.
- Conflicting default methods from two interfaces must be overridden explicitly.
Practice what you learned
1. What is the default access modifier of a method declared without a body inside an interface?
2. How many classes can a single Java class extend directly?
3. How many interfaces can a single Java class implement?
4. Which Java version introduced default and static methods in interfaces?
5. What happens if a class implements two interfaces that both declare a default method with the same signature and the class does not override it?
Was this page helpful?
You May Also Like
Abstract Classes in Java
Understand Java abstract classes, how they mix abstract and concrete methods, and when to choose them over interfaces.
Polymorphism in Java
Explore compile-time and runtime polymorphism in Java, including method overloading, overriding, and dynamic dispatch.
Packages in Java
Learn how Java packages organize classes into namespaces, how the package and import statements work, and how package-private access controls visibility.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics