DEV Community

Dhanush
Dhanush

Posted on

1 1 1

What is the difference between static and default methods in a Java interface?

Static methods

Static methods in interfaces are methods that belong to the interface itself, not to any instance of a class that implements the interface. They can't be overridden by implementing classes, right? You call them using the interface name, like InterfaceName.staticMethod().

Default methods

Default methods, on the other hand, provide a default implementation that implementing classes can use or override. They were introduced in Java 8 to allow adding new methods to interfaces without breaking existing implementations. So if a class doesn't override a default method, it gets the default behavior.

For example, adding a new method to an existing interface where all existing implementations can use the default until they decide to implement their own.

interface Vehicle {  
// Static method  
static String getEngineInfo() 
{ 
return "Generic Engine"; 
}  

// Default method  
default void honk() 
{ 
System.out.println("Beep!"); 
}  
}  

// Usage  
class Car implements Vehicle {}  
Car car = new Car();  
car.honk();                // Output: Beep! (uses default)  
String info = Vehicle.getEngineInfo(); // Calls static method  
Enter fullscreen mode Exit fullscreen mode

There is no need for getEngineInfo() method or honk() method to override in the implementing classes.
Refer: stackoverflow

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

VS Code, Live - Vibe Coding @ Build Day 2

Join us at Microsoft Build where we will be vibe coding all day with Visual Studio Code and GitHub Copilot!

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️