What is an Interface?
An interface in Java is a collection of abstract methods (methods without a body) and constants (variables marked as final and static). It is mainly used to achieve abstraction and multiple inheritance in Java.
In Java, an interface is like a plan or agreement that says what a class should do, but not how to do it. You can think of it like a promise; if a class agrees to use an interface, it must keep the promise and do all the work written in that interface.
Why Interface Is Needed in Java?
Sometimes different classes share similar behaviors, but they belong to different hierarchies. For example, both a Bird and an aeroplane can fly, but they are not related by inheritance.
In such cases, we can create an interface like Flyable, and both Bird and Aeroplane can implement it. This makes your code flexible and organized.
Features of Interfaces in Java
- Achieves Complete Abstraction: Interfaces allow you to hide all the implementation details and only show the method names.
- Multiple Inheritance Support: In Java, a class cannot inherit from more than one class, but it can implement multiple interfaces. This allows a single class to take features or rules from many sources.
- Method Declarations: Interfaces contain only method declarations, not method bodies. This means the actual code for the method is written in the class that implements the interface.
- Constants Only: All variables declared inside an interface are automatically public, static, and final. That means they are constant values that cannot be changed later.
- Default and Static Methods: From Java 8 onwards, interfaces can also have default methods and static methods.
- Functional Interfaces: Introduced in Java 8, a functional interface contains only one abstract method. They are often used with lambda expressions to make code shorter and cleaner.
Syntax of an Interface in Java
interface InterfaceName {
// Abstract method (no body)
void method1();
// Default method (has a body, added in Java 8)
default void method2() {
System.out.println("This is a default method");
}
// Static method (belongs to interface itself)
static void method3() {
System.out.println("This is a static method");
}
// Constant (automatically public, static, and final)
int CONSTANT_VALUE = 100;
}
Let’s Understand Each Part in Simple Words
1) Abstract Method: This method is declared but not defined (no body), and the implementing class must provide the method body.
For example:
void method1();
2) Default Method: It allows interfaces to provide a default behavior. Classes can use it directly or override it if needed. For example:
default void method2() {
System.out.println("This is a default method");
}
3) Static Method: It belongs to the interface itself, not to any class or object. You can call it using the interface name directly. For example:
static void method3() {
System.out.println("This is a static method");
}
4) Constant: Any variable inside an interface is automatically public, static, and final,
meaning it cannot be changed after being declared. Example:
int CONSTANT_VALUE = 100;
How to Implement an Interface
A class must use the implements keyword to define the behavior of the methods declared in an interface.
Example of Interface Implementation
interface Device {
// Abstract method
void start();
// Default method
default void stop() {
System.out.println("Device is stopping...");
}
// Static method
static void info() {
System.out.println("All devices must follow safety guidelines.");
}
// Constant
int MAX_BATTERY = 100;
}
class Laptop implements Device {
public void start() {
System.out.println("Laptop is starting with " + MAX_BATTERY + "% battery.");
}
}
public class Main {
public static void main(String[] args) {
Laptop myLaptop = new Laptop();
myLaptop.start(); // Calls abstract method implementation
myLaptop.stop(); // Uses default method from interface
Device.info(); // Calls static method from interface
}
}
Output:
Laptop is starting with 100% battery.
Device is stopping...
All devices must follow safety guidelines.
Default and Static Methods in Interfaces
However, from Java 8 onwards, Java introduced default and static methods inside interfaces. This made interfaces much more powerful and flexible.
- Default Methods: A default method is a method in an interface that has a body (implementation). It is defined using the default keyword.
- Static Methods: A static method belongs to the interface itself, not to the objects or classes that implement it.
Example: Default and Static Methods
interface Appliance {
void turnOn(); // Abstract method
// Default method
default void turnOff() {
System.out.println("Appliance is turning off...");
}
// Static method
static void powerCheck() {
System.out.println("Power supply is stable.");
}
}
class Fan implements Appliance {
@Override
public void turnOn() {
System.out.println("Fan starts rotating...");
}
// Overriding the default method
@Override
public void turnOff() {
System.out.println("Fan is now turned off.");
}
}
public class Main {
public static void main(String[] args) {
Fan myFan = new Fan();
myFan.turnOn(); // Abstract method implemented in Fan
myFan.turnOff(); // Overridden default method
Appliance.powerCheck(); // Static method called using interface name
}
}
Output:
Fan starts rotating...
Fan is now turned off.
Power supply is stable.
Functional Interfaces in Java
A functional interface in Java is an interface that contains only one abstract method. It defines a single task or behavior that can be implemented using a lambda expression or a method reference.
Think of a functional interface like a single-purpose contract.
It tells a class or a lambda expression: “I just need you to perform one specific operation.”
It can be annotated with @FunctionalInterface (optional, but recommended). This annotation ensures that the compiler will show an error if you accidentally add more than one abstract method.
Example: Functional Interface with Lambda Expressions
@FunctionalInterface
interface MessagePrinter {
void printMessage(String message); // Single abstract method
}
public class Main {
public static void main(String[] args) {
// Using lambda expression to define the behavior
MessagePrinter greet = (msg) -> System.out.println("Greeting: " + msg);
MessagePrinter warn = (msg) -> System.out.println("Warning: " + msg);
// Calling the lambda implementations
greet.printMessage("Welcome to Java Functional Interfaces!");
warn.printMessage("Low battery, please plug in the charger!");
}
}
Output:
Greeting: Welcome to Java Functional Interfaces!
Warning: Low battery, please plug in the charger!
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.
- What is Java if else statements?
- What is method overloading in Java?
- What are the Classes and Objects in Java?
- What is Polymorphism in Java?
- What is Interfaces in Java?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.