Basics of Interface
An interface is a contract that defines what a class must do, without requiring a specific implementation.
Interface = what to do
Class = how to do it
1. Creating an Interface
Use the interface keyword.
interface Animal {
void sound();
void move();
}
The methods above are abstract methods by default.
2. Implementing an Interface
A class uses implements.
class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
public void move() {
System.out.println("Running");
}
}
Now:
Dog dog = new Dog();
dog.sound();
dog.move();
Output:
Woof
Running
Important
Interface methods must be implemented as public.
public void sound() {
}
You cannot reduce their visibility to protected or private.
3. Why Use Interfaces?
Interfaces allow different classes to follow the same contract while having completely different implementations.
interface Flyable {
void fly();
}
Different classes can implement it:
class Bird implements Flyable {
public void fly() {
System.out.println("Bird flies");
}
}
class Airplane implements Flyable {
public void fly() {
System.out.println("Airplane flies");
}
}
Both follow:
Flyable
↓
fly()
but implement it differently.
4. Interface Reference
You can use an interface as a reference type.
Flyable f = new Bird();
f.fly();
This is also polymorphism.
Flyable f1 = new Bird();
Flyable f2 = new Airplane();
f1.fly(); // Bird flies
f2.fly(); // Airplane flies
5. Interface Cannot Be Instantiated
You cannot create an object directly from an interface.
Flyable f = new Flyable(); // ERROR
You need a class that implements it:
Flyable f = new Bird(); // OK
6. Interface Constants
Variables declared in an interface are automatically:
public static final
Example:
interface MathConstants {
double PI = 3.14159;
}
This is effectively:
public static final double PI = 3.14159;
Use it:
System.out.println(MathConstants.PI);
You cannot change it:
MathConstants.PI = 10; // ERROR
7. Default Methods
A default method has a body.
interface Animal {
void sound();
default void eat() {
System.out.println("Eating");
}
}
A class only needs to implement the abstract method:
class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
}
Dog automatically gets eat():
Dog dog = new Dog();
dog.sound();
dog.eat();
Output:
Woof
Eating
Why default methods?
They allow new behavior to be added to an interface without forcing every existing implementing class to implement it.
8. Static Methods
Interfaces can have static methods.
interface MathUtil {
static int add(int a, int b) {
return a + b;
}
}
Call it using the interface:
System.out.println(MathUtil.add(10, 20));
Output:
30
You cannot call it through an object:
MathUtil.add(); // interface method
Interface static methods are not inherited by implementing classes.
9. Private Methods
Interfaces can have private methods for internal code reuse.
interface Animal {
default void eat() {
printMessage();
}
default void sleep() {
printMessage();
}
private void printMessage() {
System.out.println("Animal action");
}
}
The private method can only be used inside the interface.
Implementing classes cannot call it directly.
10. Interface Can Extend Multiple Interfaces
A class can extend only one class, but an interface can extend multiple interfaces.
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
interface DuckBehavior extends Flyable, Swimmable {
}
A class implementing DuckBehavior must implement both:
class Duck implements DuckBehavior {
public void fly() {
System.out.println("Flying");
}
public void swim() {
System.out.println("Swimming");
}
}
11. Interfaces as APIs
Interfaces are commonly used to create flexible APIs.
For example:
interface Payment {
void pay(double amount);
}
Different payment systems can implement it:
class CreditCard implements Payment {
public void pay(double amount) {
System.out.println("Paid by credit card");
}
}
class UPI implements Payment {
public void pay(double amount) {
System.out.println("Paid using UPI");
}
}
The rest of the application only needs to know about:
Payment
It doesn't need to know the internal implementation.
Quick Revision
| Feature | Interface |
|---|---|
| Keyword | interface |
| Object creation | ❌ Cannot instantiate directly |
| Abstract methods | ✅ |
default methods |
✅ |
| Static methods | ✅ |
| Private methods | ✅ |
| Variables | public static final |
| Constructors | ❌ |
| Multiple inheritance | ✅ Can extend multiple interfaces |
| Class relationship | implements |
| Main purpose | Define a contract |
Easy Rule
Interface = contract / what a class should do.
Flyable
↓
fly()
/ \
Bird Airplane
↓ ↓
how it how it
flies flies
The interface defines what must be done; each class decides how to do it.