What is an Interface?
An interface is a set of rules that a class must follow.
Simple Definition
An interface tells a class what to do, not how to do it.
The how part is written by the class that implements the interface.
Why Do We Use Interface?
Interfaces are mainly used to:
- Achieve 100% abstraction
- Support multiple inheritance
- Define common behavior across different classes
- Enforce a standard structure in applications
How to Create an Interface?
We use the interface keyword.
Syntax:
interface InterfaceName {
// abstract methods
}
Important Rules of Interface (Before Java 8)
- An interface contains only abstract methods
- All methods are public and abstract by default
- All variables are public static final by default
- We cannot create an object for an interface
- We cannot create a constructor inside an interface
Simple Interface Example
interface ATM {
void withdraw();
void deposit();
}
Explanation:
- Methods are automatically
public abstract - No method body is allowed
- No constructor is allowed
Implementing an Interface
A class uses the implements keyword to implement an interface.
class IciciBank implements ATM {
@Override
public void withdraw() {
System.out.println("Withdraw money");
}
@Override
public void deposit() {
System.out.println("Deposit money");
}
}
Explanation:
-
IciciBankimplementsATM - All interface methods must be implemented
- If not, the class must be declared abstract
Using the Implemented Interface
public class User {
public static void main(String[] args) {
ATM atm = new IciciBank();
atm.withdraw();
atm.deposit();
}
}
Explanation:
- Interface reference can point to class object
- Achieves runtime flexibility
- Implementation details remain hidden
Interface and Multiple Inheritance
Java does not support multiple inheritance using classes,
but it supports multiple inheritance using interfaces.
Example:
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
Interface After Java 8
From Java 8 onwards, interfaces can have method body using:
1. Default Methods
interface Bank {
default void welcome() {
System.out.println("Welcome");
}
}
Explanation:
-
defaultkeyword allows method body - Helps in adding new methods without breaking existing code
- Can be overridden in implementing class
2. Static Methods in Interface
interface Bank {
static void rules() {
System.out.println("Bank Rules");
}
}
How to Call Static Method?
Bank.rules();
Important Points:
- Static methods are called using interface name
- Static methods cannot be overridden
- Belong to the interface, not to the class
Key Differences: Abstract Class vs Interface
| Feature | Abstract Class | Interface |
|---|---|---|
| Object creation | ❌ No | ❌ No |
| Constructor | ✔ Yes | ❌ No |
| Multiple inheritance | ❌ No | ✔ Yes |
| Method body | ✔ Yes | ❌ (Before Java 8) |
| Abstraction | Partial | 100% |
Key Points to Remember
- Interface is a blueprint / rulebook
- Achieves 100% abstraction
- Uses
implementskeyword - Supports multiple inheritance
- Cannot create object or constructor
- Java 8 introduced default and static methods
When to Use Interface?
Use interface when:
- You need multiple inheritance
- You want complete abstraction
- You want to define common behavior
- You want loose coupling in applications
Top comments (0)