Explanation of Interfaces in Java

Lets get started
What Is an Interface?
In Java, Interfaces are one of the interfaces are a fundamental building block of object-oriented programming. Interfaces specify the methods which a derived class must implement but they dont know how they are implemented. Interfaces are powerful way to achieve abstraction, polymorphism and multiple inheritance in Java.
Lets define an interface now:
interface Vehicle {
void start(); // abstract method
}
Any class that implements this interface must provide an implementation for start().
Implementing Interfaces
A class uses the implements keyword to implement an interface.
interface Vehicle {
void start();
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Start the car!");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
car.start(); // Output: Start the car!
}
}
In this wa, we have achieved polymorphism by the definition of Vehicle interface and we can initialise it with different type of Similar classes.
Multiple Interfaces
A Java class can implement multiple interfaces.
interface Human {
void speak();
}
interface Animal {
void run();
}
class Thing implements Human, Animal {
public void speak() {
System.out.println("Thing speaking");
}
public void run() {
System.out.println("Thing running");
}
}
In this way Java is supporting multiple inheritance.
Keep in mind, A Java class can implement multiple interfaces, but can not extend multiple classes.
Interface Inheritance
Just like classes, interfaces can extend other interfaces.
interface Vehicle {
void start();
}
interface ElectricVehicle extends Vehicle {
void chargeBattery();
}
class Togg implements ElectricVehicle {
public void start() {
System.out.println("Togg starting silently...");
}
public void chargeBattery() {
System.out.println("Charging Togg battery...");
}
}
In this way we can build a hierarchy between the interfaces and the classes. And also we can produce reusable abstract interfaces.
From Java 8+, interfaces can contain default and static methods with a body.
Default Method Example in Interface
interface Logger {
default void log(String message) {
System.out.println("Log: " + message);
}
}
class ConsoleLogger implements Logger {
// Inherits log() method from Logger
}
Static Method example in Interface
interface MathUtil {
static int add(int a, int b) {
return a + b;
}
}
public class Test {
public static void main(String[] args) {
System.out.println(MathUtil.add(5, 3)); // Output: 8
}
}
Functional Interfaces and Lambdas
A functional interface is an interface with only one abstract method. It is the foundation for lambda expressions and method references.
Functional Interface Example:
@FunctionalInterface
interface Calculator {
int operate(int a, int b);
}
public class LambdaDemo {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
System.out.println(add.operate(10, 5)); // Output: 15
}
}
The @FunctionalInterface annotation is optional but it helps catch errors at compile-time if the interface contains more than one abstract method.
Summary
Interfaces are really efficient way to achieve absgtraction and also polymorphism. With the lates improvements of Interfaces like Functional Interfaces, default methods and more, it has more support for modern software development with Java. For Java development, mastering interfaces is really crucial.
That is all.
Happy coding.
Burak Hamdi TUFAN