Java Interface
Contracts that define what classes must implement
🤝 What is an Interface?
An interface is a contract that defines methods a class must implement. It achieves 100% abstraction and supports multiple inheritance, making code more flexible and organized.
// Interface definition
interface Drawable {
void draw(); // Abstract method (public by default)
default void display() { System.out.println("Displaying..."); }
}
Interface Features
Contract
Defines what methods must be implemented
interface Flyable {
void fly();
}
Multiple Inheritance
A class can implement multiple interfaces
class Bird implements Flyable, Drawable {
// Implementation
}
100% Abstraction
All methods are abstract by default
interface Calculator {
int add(int a, int b);
int subtract(int a, int b);
}
Constants
Variables are public, static, final
interface Constants {
int MAX_SIZE = 100;
String APP_NAME = "MyApp";
}
🔹 Basic Interface Example
Here's how to create and implement interfaces:
// Interface definition
interface Animal {
// Abstract methods (public by default)
void makeSound();
void move();
// Constants (public, static, final by default)
String CATEGORY = "Living Being";
}
// Class implementing interface
class Dog implements Animal {
private String name;
Dog(String name) {
this.name = name;
}
// Must implement all abstract methods
public void makeSound() {
System.out.println(name + " says: Woof!");
}
public void move() {
System.out.println(name + " runs on four legs");
}
}
class Cat implements Animal {
private String name;
Cat(String name) {
this.name = name;
}
public void makeSound() {
System.out.println(name + " says: Meow!");
}
public void move() {
System.out.println(name + " walks silently");
}
}
Usage:
Dog dog = new Dog("Buddy");
dog.makeSound(); // Buddy says: Woof!
dog.move(); // Buddy runs on four legs
Cat cat = new Cat("Whiskers");
cat.makeSound(); // Whiskers says: Meow!
cat.move(); // Whiskers walks silently
System.out.println(Animal.CATEGORY); // Living Being
🔹 Multiple Interface Implementation
A class can implement multiple interfaces for greater flexibility:
interface Swimmer {
void swim();
}
interface Flyer {
void fly();
}
interface Runner {
void run();
}
// Duck can swim, fly, and run
class Duck implements Swimmer, Flyer, Runner {
private String name;
Duck(String name) {
this.name = name;
}
public void swim() {
System.out.println(name + " swims in water");
}
public void fly() {
System.out.println(name + " flies in the sky");
}
public void run() {
System.out.println(name + " runs on land");
}
}
// Fish can only swim
class Fish implements Swimmer {
private String name;
Fish(String name) {
this.name = name;
}
public void swim() {
System.out.println(name + " swims underwater");
}
}
Output:
Duck duck = new Duck("Donald");
duck.swim(); // Donald swims in water
duck.fly(); // Donald flies in the sky
duck.run(); // Donald runs on land
Fish fish = new Fish("Nemo");
fish.swim(); // Nemo swims underwater
🔹 Default and Static Methods
Java 8 introduced default and static methods in interfaces:
Modern Interface Features:
- Default Methods: Methods with implementation in interface
- Static Methods: Utility methods that belong to interface
- Private Methods: Helper methods (Java 9+)
interface Vehicle {
// Abstract method
void start();
// Default method - provides default implementation
default void honk() {
System.out.println("Beep beep!");
}
// Static method - utility method
static void checkFuel() {
System.out.println("Checking fuel level...");
}
// Private method (Java 9+) - helper method
private void log(String message) {
System.out.println("Log: " + message);
}
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started with key");
}
// Can override default method if needed
@Override
public void honk() {
System.out.println("Car horn: Honk honk!");
}
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike started with kick");
}
// Uses default honk() method
}
Usage:
Car car = new Car(); car.start(); // Car started with key car.honk(); // Car horn: Honk honk! Bike bike = new Bike(); bike.start(); // Bike started with kick bike.honk(); // Beep beep! (default implementation) Vehicle.checkFuel(); // Checking fuel level... (static method)
🔹 Interface vs Abstract Class
Understanding when to use interfaces vs abstract classes:
Interface:
- 100% abstraction (except default methods)
- Multiple inheritance supported
- Only public methods
- Variables are public, static, final
- Cannot have constructors
Abstract Class:
- 0-100% abstraction
- Single inheritance only
- Can have any access modifier
- Can have instance variables
- Can have constructors