⬅ Previous Topic
Java Abstraction in OOPNext Topic ⮕
Polymorphism in Java⬅ Previous Topic
Java Abstraction in OOPNext Topic ⮕
Polymorphism in JavaA Java Interface is a blueprint for a class. It defines what a class must do, but not how it should do it. Think of it as a contract — any class that signs this contract must fulfill its promises (implement the methods declared in the interface).
Interfaces help achieve abstraction and multiple inheritance.
Interfaces in Java are not just a technical feature; they represent a powerful design choice that guides how your code is structured and how different parts of your application communicate. Here’s why interfaces matter:
One of the primary reasons to use interfaces is to achieve complete abstraction. Unlike abstract classes, which can contain both implemented and non-implemented methods, interfaces (at least before Java 8) allow only method declarations. This means you’re defining what an object should do—not how it should do it.
By doing this, you’re creating a clean, minimal contract that any implementing class must fulfill. The details are left to the individual classes, giving your code more clarity, modularity, and consistency.
Java doesn’t support multiple inheritance with classes to avoid the infamous “Diamond Problem.” But interfaces are the workaround. A class in Java can implement multiple interfaces, effectively inheriting behavior from multiple sources—without the ambiguity of conflicting implementations.
This enables powerful design patterns where one class can simultaneously behave like multiple types. It’s one of the key reasons interfaces are used extensively in large-scale Java systems.
Interfaces help in reducing the tight coupling between different components of your code. When one part of your system interacts with another through an interface, it doesn’t need to know the actual implementation—it just relies on the contract.
This decoupling makes your code more flexible and easier to test, extend, or replace. You can switch out the implementation without changing the interface, which is especially useful in large teams, enterprise-level systems, or when applying design patterns like Strategy, Dependency Injection, or Factory.
Use the interface
keyword:
interface Animal {
void makeSound();
}
Here, makeSound()
is an abstract method — it has no body. Any class that implements Animal
must provide its own version of makeSound()
.
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
Bark
The Dog
class implements Animal
and provides its own behavior for makeSound()
.
A class can implement more than one interface. This is how Java achieves multiple inheritance.
interface Walkable {
void walk();
}
interface Swimmable {
void swim();
}
class Duck implements Walkable, Swimmable {
public void walk() {
System.out.println("Duck walks");
}
public void swim() {
System.out.println("Duck swims");
}
}
Duck walks
Duck swims
public static final
(constants)public abstract
by default (until Java 8)interface Constants {
int MAX_USERS = 100;
}
You can access MAX_USERS
like this:
System.out.println(Constants.MAX_USERS);
From Java 8 onward, interfaces can also have default
and static
methods with bodies.
interface Vehicle {
default void start() {
System.out.println("Starting vehicle...");
}
static void info() {
System.out.println("Vehicle Interface");
}
}
class Car implements Vehicle {}
Car c = new Car();
c.start(); // default method
Vehicle.info(); // static method
Starting vehicle...
Vehicle Interface
A Marker Interface is an interface with no methods or fields. It acts as a tag for the class.
interface Serializable {} // Java’s built-in marker interface
Interfaces with exactly one abstract method are called Functional Interfaces. They enable usage with lambda expressions.
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
Used like this:
Calculator add = (a, b) -> a + b;
System.out.println(add.operate(5, 3));
8
Use an interface when:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
}
}
⬅ Previous Topic
Java Abstraction in OOPNext Topic ⮕
Polymorphism in JavaYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.