What is the difference between static and default methods in a Java interface?
Solution 1
Differences between static and default methods in Java 8:
1) Default methods can be overriden in implementing class, while static cannot.
2) Static method belongs only to Interface class, so you can only invoke static method on Interface class, not on class implementing this Interface, see:
public interface MyInterface {
default void defaultMethod(){
System.out.println("Default");
}
static void staticMethod(){
System.out.println("Static");
}
}
public class MyClass implements MyInterface {
public static void main(String[] args) {
MyClass.staticMethod(); //not valid - static method may be invoked on containing interface class only
MyInterface.staticMethod(); //valid
}
}
3) Both class and interface can have static methods with same names, and neither overrides other!
public class MyClass implements MyInterface {
public static void main(String[] args) {
//both are valid and have different behaviour
MyClass.staticMethod();
MyInterface.staticMethod();
}
static void staticMethod(){
System.out.println("another static..");
}
}
Solution 2
A static method is a method that applies to the class 'namespace', so to speak. So a static
method foo
of interface Interface
is accessed by Interface.foo()
. Note that the function call does not apply to any particular instance of the interface.
A default implementation bar
on the other hand, is called by
Interface x = new ConcreteClass();
x.bar();
A static
interface method cannot know about the this
variable, but a default implementation can.
Solution 3
1. explain the difference of the two
Static interface methods are like static class methods(here they belong to Interface only). Where as the default interface methods provide default implementation
of interface methods (which implementing classes may override
)
But remember in case a class is implementing more than one interface with same default
method signature then the implementing class needs to override the default method
You can find a simple example below (can DIY for different cases)
public class Test {
public static void main(String[] args) {
// Accessing the static member
I1.hello();
// Anonymous class Not overriding the default method
I1 t = new I1() {
@Override
public void test() {
System.out.println("Anonymous test");
}
};
t.test();
t.hello("uvw");
// Referring to class instance with overridden default method
I1 t1 = new Test2();
t1.test();
t1.hello("xyz");
}
}
interface I1 {
void test();
//static method
static void hello() {
System.out.println("hello from Interface I1");
}
// default need not to be implemented by implementing class
default void hello(String name) {
System.out.println("Hello " + name);
}
}
class Test2 implements I1 {
@Override
public void test() {
System.out.println("testing 1234...");
}
@Override
public void hello(String name) {
System.out.println("bonjour" + name);
}
}
2. when we would use this would be nice.
That depends on your problem statement. I would say Default methods are useful, if you need same implementation for a method in your specification in all the classes in that contract, Or it may be used like Adapter
classes.
here is a good read: https://softwareengineering.stackexchange.com/questions/233053/why-were-default-and-static-methods-added-to-interfaces-in-java-8-when-we-alread
also below oracle doc explains default & static methods for evolving existing interfaces:
Users who have classes that implement interfaces enhanced with new default or static methods do not have to modify or recompile them to accommodate the additional methods.
http://docs.oracle.com/javase/tutorial/java/IandI/nogrow.html
Solution 4
Here is my view:
static method in interface:
You can call it directly (InterfacetA.staticMethod())
Sub-class will not be able to override.
Sub-class may have method with same name as staticMethod
default method in interface:
You can not call it directly.
Sub-class will be able to override it
Advantage:
static Method: You don't need to create separate class for utility method.
default Method: Provide the common functionality in default method.
Solution 5
This link has some useful insights, have listed few of them here.
default & static methods have bridged down the differences between interfaces and abstract classes.
Interface default methods:
- It helps in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
- It helps in extending interfaces without having the fear of breaking implementation classes.
Interface static methods:
- They are part of interface, we can’t use it for implementation class objects.
- It helps in providing security by not allowing implementation classes to override them.
Like to quote another useful reference.
Vipin Menon
Updated on December 08, 2021Comments
-
Vipin Menon over 2 years
I was learning through interfaces when I noticed that you can now define static and default methods in an interface.
public interface interfacesample2 { public static void method() { System.out.println("hello world"); } public default void menthod3() { System.out.println("default print"); } }
Kindly explain the difference of the two and also if there's an example of when we would use this would be nice. A little confused on Interfaces.
-
Dawood ibn Kareem over 9 yearsDid you try reading up on static methods in the Java tutorial?
-
Dawood ibn Kareem over 9 yearsSo you missed the part about not ever being able to override a static method?
-
Vipin Menon over 9 yearsdidnt understand the same on interfaces
-
Shail016 over 9 yearsstatic method is a static member to the Interface, cant be overridden (as with the class), default method is the
default implementation
of a method which might be overridden. -
GhostCat over 7 yearsJust wondering: why did you never accept an answer here?
-
-
Shashank Vivek over 6 yearsbut why 'static' ? what purpose does it serve in Java 8?
-
stinger over 6 yearsPurpose of static keyword doesn't changed-- to define class level members: fields, methods etc. In Java 8 this behavior was expanded to Interfaces, so they become more similar to classes and now can replace class in most scenarios.
-
amarnath harish almost 6 yearsyea but we can still hide the interface static method instead of overriding,....i just think both fulfills the same
purpose
(use a common implementation) and resolves ambiguity byimplementing the logic again in subclass
(overriding,hiding). the only sensible reason would be due to the reason that[ static interface methods are not inherited](stackoverflow.com/questions/25169175/…) and hence we cant call them using subclass instance. -
amarnath harish almost 6 yearsthis answer seems like it was going somewhere good,,then suddenly boom ! no meaningful explanation at the end. but not changed interface implementation what does this mean?
-
Hackinet over 5 yearsI have a doubt. Is it possible to create a object of a interface? Your code has this line: I1 t = new I1()
-
Shail016 over 5 years@Hackinet kindly read the java comment over that statement. Please also read about Anonymous classes. Hope that helps you.