An interface in Java is a reference type that defines a contract containing methods and constants that implementing classes can use. It helps achieve abstraction and allows different classes to provide their own implementations of common behavior.
- Defines common behavior without specifying its complete implementation.
- Allows a class to implement multiple interfaces.
Real-Life Example
Consider a payment system that supports UPI and card payments. Both payment methods perform the same operation, but their implementations are different.

interface Payment{
void pay(int amount);
}
// UPI payment implementation
class UPIPayment implements Payment{
public void pay(int amount){
System.out.println("Paid " + amount + " using UPI");
}
}
// Card payment implementation
class CardPayment implements Payment{
public void pay(int amount){
System.out.println("Paid " + amount + " using Card");
}
}
public class Geeks{
public static void main(String[] args){
Payment p1 = new UPIPayment();
p1.pay(500);
Payment p2 = new CardPayment();
p2.pay(1000);
}
}
Output
Paid 500 using UPI Paid 1000 using Card
Explanation:
- Payment declares the pay(int amount) method.
- UPIPayment and CardPayment provide their own implementations.
- The interface reference allows both objects to be accessed using the same type.
Notes: The interface variable a is automatically public static final, and display() is automatically
public abstract.
Relationship Between Class and Interface
Java supports the following relationships between classes and interfaces:
- A class can extend only one class.
- A class can implement multiple interfaces.
- An interface can extend one or more interfaces.
- An interface cannot implement a class or another interface.

Use Cases of Class and Interface
Class
- Use a class to represent entities with properties and behaviors.
- Use a class when objects need to maintain their own state.
Interface
- Use an interface to define common behavior for unrelated classes.
- Use an interface when multiple classes need to follow the same contract.
Implementing an Interface
A class implements an interface using the implements keyword and provides implementations for its abstract methods.
Syntax:
class ClassName implements InterfaceName{
// Implement interface methods
}
A class can also implement multiple interfaces by separating their names with commas.
Multiple Inheritance Using Interfaces
Java does not support multiple inheritance through classes, but a class can implement multiple interfaces.
interface Add{
int add(int a, int b);
}
interface Sub{
int sub(int a, int b);
}
class Cal implements Add, Sub{
public int add(int a, int b){
return a + b;
}
public int sub(int a, int b){
return a - b;
}
}
public class Geeks{
public static void main(String[] args){
Cal obj = new Cal();
System.out.println("Addition: " + obj.add(2, 1));
System.out.println("Subtraction: " + obj.sub(2, 1));
}
}
Output
Addition: 3 Subtraction: 1
Explanation: Cal implements both Add and Sub interfaces and provides implementations for their methods. This demonstrates multiple inheritance of type through interfaces.
Interface Features and Enhancements in Java 8
Java 8 introduced default and static methods in interfaces. These features allow interfaces to provide method implementations in addition to abstract method declarations.
1. Default Methods
A default method is a method declared using the default keyword that contains an implementation.
- Can be inherited by implementing classes.
- Can be overridden by implementing classes.
interface TestInterface{
default void display(){
System.out.println("Hello from default method");
}
}
class TestClass implements TestInterface{
public static void main(String[] args){
TestClass obj = new TestClass();
obj.display();
}
}
Output
Hello from default method
Explanation: The interface provides an implementation of display(). TestClass inherits and calls the method without overriding it.
2. Static Methods
A static method belongs to the interface and is called using the interface name. It is not inherited by implementing classes.
interface TestInterface{
static void display(){
System.out.println("Hello from static method");
}
}
public class Geeks{
public static void main(String[] args){
TestInterface.display();
}
}
Output
Hello from static method
Explanation: The display() method is called directly using the interface name without creating an object.3.
3. Functional Interfaces
A functional interface contains exactly one abstract method and can be used with lambda expressions and method references.
New Features Added in Interfaces in JDK 9
Java 9 introduced private methods in interfaces to allow code reuse within default and static methods.
Private Methods
- Private methods can contain implementations.
- They can be called only from other methods within the same interface.
- They cannot be accessed or overridden by implementing classes.
interface Vehicle{
private void startEngine(){
System.out.println("Engine started");
}
default void drive(){
startEngine();
System.out.println("Vehicle is moving");
}
}
class Car implements Vehicle{
}
public class Geeks{
public static void main(String[] args){
Car car = new Car();
car.drive();
}
}
Output
Engine started Vehicle is moving
Explanation: The private startEngine() method is called internally by the default drive() method. The implementing class can use drive() but cannot directly access startEngine().
Extending Interfaces
An interface can extend another interface using the extends keyword. The child interface inherits the abstract method declarations of its parent interface.
interface A{
void method1();
}
interface B extends A{
void method2();
}
class Geeks implements B{
public void method1(){
System.out.println("Method 1");
}
public void method2(){
System.out.println("Method 2");
}
public static void main(String[] args){
Geeks obj = new Geeks();
obj.method1();
obj.method2();
}
}
Output
Method 1 Method 2
Explanation: Interface B extends interface A and inherits its method declaration. Therefore, Geeks must implement both method1() and method2().

