Java Interfaces Explained: How to Define and Implement Them with Practical Examples
What Is a Java Interface?
A Java interface is an abstract type that defines a contract for classes. It can declare constants and abstract methods but cannot hold state. All methods are implicitly public and abstract, and a class can implement multiple interfaces, providing the required behavior.
Declaring an Interface
interface MyInterface {
// abstract methods
}
Implementing an Interface
class Dog implements Pet {
// implement methods from Pet
}
Extending Interfaces
interface RidableAnimal extends Animal, Vehicle {
// additional abstract methods
}
Why Are Interfaces Needed?
Interfaces allow you to separate the what from the how. They enable polymorphism, promote loose coupling, and solve the “multiple inheritance” problem that Java does not support for classes.
For example, a MediaPlayer may have separate CDPlayer and DVDPlayer subclasses, each providing its own play() implementation. If a class needed both capabilities, Java would forbid multiple inheritance of classes, but an interface lets us combine the behaviors cleanly.


Similarly, a Dog class can extend Animal and implement a Pet interface to satisfy both inheritance and contractual obligations.

Java Interface Example
Step 1: Write the following code:
interface Pet {
void test();
}
class Dog implements Pet {
@Override
public void test() {
System.out.println("Interface method implemented");
}
public static void main(String[] args) {
Pet p = new Dog();
p.test();
}
}
Step 2: Compile and run the program to see the output.
Class vs. Interface
| Class | Interface |
|---|---|
| A class can be instantiated and may contain state. | An interface cannot be instantiated and contains only constants and method signatures. |
| Can have concrete (implemented) methods. | Before Java 8, could not have concrete methods; from Java 8 onwards, may contain default and static methods. |
Access modifiers can be private, protected, or public. | All members are implicitly public (and static final for fields). |
When to Use an Interface vs. an Abstract Class?
- Abstract Class: Use when you need a common base implementation or shared state for related classes.
- Interface: Use when you need to define a role or capability that can be adopted by unrelated classes.
Key Facts About Interfaces
- A class can implement multiple interfaces, but must provide an implementation for every method declared.
- Interfaces cannot be instantiated directly; they serve as contracts.
- Interface references can point to any implementing class.
- An interface may extend one or more other interfaces.
- Nested interfaces are allowed inside other interfaces or classes.
- Two interfaces cannot declare methods with the same name but different return types if a class implements both.
- From Java 8, interfaces can contain
defaultandstaticmethods.
Summary
- Interfaces define a contract; classes provide the implementation.
- All interface methods are implicitly
publicandabstract(unlessdefaultorstatic). - Interfaces cannot be instantiated but can be referenced by variables.
- Use interfaces to achieve polymorphism and to sidestep Java’s single inheritance limitation.
Java
- Understanding C# Interfaces: Definition, Example, and Practical Use
- Understanding Java Classes and Objects: Clear Concepts, Practical Examples
- Java String length() Method: How to Get a String’s Size (Example)
- Mastering the 'this' Keyword in Java: Purpose, Usage, and Practical Examples
- Polymorphism in Java: A Comprehensive Guide with Practical Examples
- Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
- Interface vs Abstract Class in Java: How to Choose the Right Abstraction
- Mastering Java's split() Method: A Practical Guide with Code Examples
- Reading Files in Java with BufferedReader – A Practical Guide with Examples
- Master Java Reflection API: A Practical Guide with Code Examples