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 inter…


This content originally appeared on DEV Community and was authored by Dhanush

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  

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


This content originally appeared on DEV Community and was authored by Dhanush


Print Share Comment Cite Upload Translate Updates
APA

Dhanush | Sciencx (2025-02-28T08:58:45+00:00) What is the difference between static and default methods in a Java interface?. Retrieved from https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/

MLA
" » What is the difference between static and default methods in a Java interface?." Dhanush | Sciencx - Friday February 28, 2025, https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/
HARVARD
Dhanush | Sciencx Friday February 28, 2025 » What is the difference between static and default methods in a Java interface?., viewed ,<https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/>
VANCOUVER
Dhanush | Sciencx - » What is the difference between static and default methods in a Java interface?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/
CHICAGO
" » What is the difference between static and default methods in a Java interface?." Dhanush | Sciencx - Accessed . https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/
IEEE
" » What is the difference between static and default methods in a Java interface?." Dhanush | Sciencx [Online]. Available: https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/. [Accessed: ]
rf:citation
» What is the difference between static and default methods in a Java interface? | Dhanush | Sciencx | https://www.scien.cx/2025/02/28/what-is-the-difference-between-static-and-default-methods-in-a-java-interface/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.