07. Types of Interface in Java
Perfect! Let’s dive into Interfaces in Java, a core OOP concept closely tied to abstraction and multiple inheritance. I’ll explain it fully with types, rules, and examples.
Interface in Java
1. What is an Interface?
Definition: An interface is a reference type in Java that can contain abstract methods, default methods, static methods, and constants.
Purpose:
Defines a contract that implementing classes must follow.
Supports multiple inheritance (a class can implement multiple interfaces).
Provides abstraction without implementation.
2. Syntax of Interface
interface Vehicle {
int MAX_SPEED = 120; // public, static, final by default
void start(); // abstract method by default
}
Key Points:
All variables are public, static, final by default.
All methods are public and abstract by default (except default and static methods).
Cannot create objects of an interface directly.
3. Implementing an Interface
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() { // must implement start()
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car(); // Interface reference
v.start();
}
}
4. Types of Interface Methods
| Method Type | Definition | Example |
| Abstract Method | Must be implemented by implementing class | void start(); |
| Default Method (Java 8+) | Method with body in interface; can be overridden | default void fuel() { System.out.println("Fueling"); } |
| Static Method (Java 8+) | Belongs to interface; called via interface name | static void info() { System.out.println("Info"); } |
| Private Method (Java 9+) | Helper method inside interface | private void check() {} |
5. Types of Interfaces
Normal / Regular Interface
- Contains only abstract methods and constants.
interface Vehicle {
void start();
int MAX_SPEED = 120;
}
Functional Interface (Java 8+)
Contains exactly one abstract method.
Can be used with Lambda Expressions.
@FunctionalInterface
interface Drawable {
void draw();
}
Marker Interface
Empty interface, no methods.
Used to mark a class with metadata.
Examples:
Serializable,Cloneable
interface Marker {} // Marker interface
Private / Default / Static Method Interface
Java 8+: Default and static methods allowed.
Java 9+: Private methods allowed for helper methods inside interface.
6. Key Rules of Interfaces
Cannot instantiate directly:
Vehicle v = new Vehicle(); // ❌A class can implement multiple interfaces (supports multiple inheritance).
class Car implements Vehicle, Serializable {}Variables in interface are public, static, final by default.
Methods in interface are public and abstract by default (except default, static, private).
Can extend another interface.
interface ElectricVehicle extends Vehicle {}
7. Advantages of Interfaces
Multiple Inheritance: A class can implement multiple interfaces.
Abstraction: Only defines “what to do”, not “how to do”.
Loose Coupling: Interfaces allow implementation details to change without affecting dependent code.
Polymorphism: Objects can be referenced by interface type.
✅ Summary Table – Interfaces
| Type of Interface | Description | Example / Use Case |
| Normal / Regular | Abstract methods + constants | Vehicle, Shape |
| Functional | Exactly 1 abstract method, Lambda-friendly | Drawable, Comparator |
| Marker | Empty interface, metadata | Serializable, Cloneable |
| Default / Static / Private | Methods with body inside interface | Helper or reusable methods |