In java 8 default method implementation can take both public
and default
modifier.
What is the main difference between below two methods. Under which conditions which type need to follow.
default int makeMul(int x, int y) {
return x * y;
}
public default int makeMul(int x, int y) {
return x * y;
}
There's nothing special about default methods here. Java has always allowed interface methods to be declared public, even though they're already implicitly public.
From JLS 9.4:
Every method declaration in the body of an interface is implicitly public (ยง6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.
See more on this question at Stackoverflow