Till Java1.7, happily I used interfaces for implementing OOPs concept encapsulation. Means if I want to hide implementation from the end user then I will only share Interface with them and they can call my API using that interface, lets for example EJBs.
Well above was valid till Java 1.7 now in Java 8, I can write implementation of a method in Interface as well with default
and static
keyword, for example:
public interface Foo {
public abstract int someUnimplementedMethod();
default void printMsg() {
System.out.println("Hello...!");
}
static void sayHello() {
System.out.println("Hello...!");
}
}
this is perfectly valid in Java 1.8.
Now my question is:
In Java 8 are we trying to introduce something called partial encapsulation with
default
andstatic
method?I know some of you will argue, if you don't want to share implementation then better to provide
default
andstatic
implementation in subinterface and keep parent interface with abstract method, but now it is not sure that if I wrote an interface with abstract method, a newbie may write whole implementation in interface only. So it raises a question then better not to allow method implementation in interface.
If you disagree with my second point please provide a solution for that.
By the way I have read Java documentation which states:
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.