Static methods in interfaces allow an interface to define utility methods with a complete implementation. These methods belong to the interface itself and are called directly using the interface name rather than through objects of implementing classes.
- A static interface method is declared using the static keyword and must have a method body.
- It is accessed using the interface name and is neither inherited nor overridden by implementing classes.
Example: This program defines a static method in an interface and calls it using the interface name.
interface Message {
static void display()
{
System.out.println("Static method in interface");
}
void show();
}
class GFG implements Message {
public void show()
{
System.out.println(
"Abstract method implemented by class");
}
public static void main(String[] args)
{
Message.display();
GFG obj = new GFG();
obj.show();
}
}
Output
Static method in interface Abstract method implemented by class
Explanation: display() is a static method of the Message interface, so it is called using Message.display(). The show() method is an abstract interface method, so GFG provides its implementation and calls it through the object.
Syntax
interface InterfaceName {
static returnType methodName() {
// method body
}
}
Properties of Static Methods in Interfaces
1. Belong to the Interface
A static method belongs to the interface in which it is declared. It is associated with the interface rather than with objects of implementing classes.
2. Accessed Using the Interface Name
A static interface method is invoked using the interface name followed by the method name.
InterfaceName.methodName();
3. Not Inherited by Implementing Classes
Static methods declared in an interface are not inherited by classes that implement the interface. Therefore, an implementing class does not access the method through its object.
4. Cannot Be Overridden
Static interface methods cannot be overridden by implementing classes. If a class declares a static method with the same name, the two methods remain separate.
interface PrintDemo {
static void hello() {
System.out.println("Hello from interface");
}
}
class GFG implements PrintDemo {
static void hello() {
System.out.println("Hello from class");
}
public static void main(String[] args) {
PrintDemo.hello();
GFG.hello();
}
}
Output
Hello from interface Hello from class
Explanation: PrintDemo.hello() calls the method defined in the interface, while GFG.hello() calls the method defined in the class. The class method does not override the interface method.
5. Have a Complete Implementation
A static interface method must have a method body. It provides a complete implementation instead of only declaring a method signature.
interface Message {
static void display() {
System.out.println("Hello Java");
}
}
Uses of Static Interface Methods
Static interface methods are useful for utility operations that are closely related to the interface. They keep related helper functionality within the interface without requiring an implementing class or object.
- Validation: Utility methods for validating values or checking whether input follows a required format.
- Data Conversion: Helper methods for converting values into formats commonly used by the interface.
- Common Calculations: Utility methods for calculations related to the operations defined by the interface.
- Factory Operations: Static methods that create or return commonly required objects related to the interface.
- Constants and Utility Logic: Supporting methods that work with constants or common rules defined by the interface.