Java Default and Static Methods in Interfaces – Complete Guide with Examples


Learn Java interface enhancements with default and static methods introduced in Java 8, including how to use them for code reuse and maintain backward compatibility with detailed examples.

Default and Static Methods in Interfaces – Complete Detailed Tutorial

Before Java 8, interfaces could only have abstract methods.

  1. Default methods and static methods were introduced in Java 8 to provide method implementations in interfaces without breaking existing code.

1. Default Methods in Interfaces

  1. Declared using the default keyword
  2. Provides a method body inside the interface
  3. Can be overridden by implementing classes
  4. Helps in backward compatibility

Syntax:


interface MyInterface {
default void greet() {
System.out.println("Hello from default method");
}
}

2. Example – Default Method


interface Vehicle {
default void start() {
System.out.println("Vehicle starts");
}
}

class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car starts with key");
}
}

class Bike implements Vehicle {
// uses default start method
}

public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start(); // overridden method

Bike bike = new Bike();
bike.start(); // default method
}
}

Output:


Car starts with key
Vehicle starts

Explanation:

  1. Car overrides the default method
  2. Bike uses the default method provided by interface

3. Static Methods in Interfaces

  1. Declared using the static keyword
  2. Called using interface name, not object reference
  3. Cannot be overridden by implementing classes
  4. Useful for utility methods related to interface

Syntax:


interface Calculator {
static int add(int a, int b) {
return a + b;
}
}

4. Example – Static Method


interface Calculator {
static int add(int a, int b) {
return a + b;
}

static int multiply(int a, int b) {
return a * b;
}
}

public class Main {
public static void main(String[] args) {
System.out.println("Addition: " + Calculator.add(10, 20));
System.out.println("Multiplication: " + Calculator.multiply(5, 6));
}
}

Output:


Addition: 30
Multiplication: 30

Explanation:

  1. Static methods are called using interface name
  2. Cannot be accessed through implementing class object

5. Advantages of Default and Static Methods

Default Methods:

  1. Provide method implementation in interfaces
  2. Maintain backward compatibility
  3. Can be overridden by implementing class

Static Methods:

  1. Provide utility/helper methods
  2. No need for object instantiation
  3. Improve modularity and code organization

6. Rules for Default and Static Methods

  1. Default methods can be overridden; static methods cannot
  2. Default method conflict resolution: if multiple interfaces have the same default method, class must override it
  3. Static methods cannot be called using class object, only using interface name

7. Example – Resolving Default Method Conflict


interface A {
default void show() {
System.out.println("Interface A");
}
}

interface B {
default void show() {
System.out.println("Interface B");
}
}

class C implements A, B {
@Override
public void show() {
System.out.println("Class C overrides show");
}
}

public class Main {
public static void main(String[] args) {
C obj = new C();
obj.show();
}
}

Output:


Class C overrides show

Explanation:

  1. When multiple interfaces have the same default method, implementing class must override

8. Summary

  1. Default methods: methods with implementation in interfaces, can be overridden
  2. Static methods: utility methods in interface, cannot be overridden
  3. Enables backward compatibility, code reuse, and modularity
  4. Essential for modern Java interface design