2

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 and static method?

  • I know some of you will argue, if you don't want to share implementation then better to provide default and static 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.

Gaurava Agarwal
  • 974
  • 1
  • 9
  • 32
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • 4
    You still can't have fields in interfaces, so you can't have state, which also means that encapsulation really has nothing to do with the changes to interfaces. – Louis Wasserman Feb 14 '16 at 19:31
  • @LouisWasserman please elaborate your comment in answer section or provide some helpful links, what you mean by "encapsulation really has nothing to do with the changes to interface", it will be helpful. thanks.. – Vishrant Feb 15 '16 at 05:55
  • Related: http://stackoverflow.com/questions/27368432/why-does-java-8-not-allow-non-public-default-methods – shmosel Jun 28 '16 at 04:25
  • 1
    *"Java1.7, happily I used interfaces for implementing OOPs concept encapsulation"* ... curious, since interfaces really don't have anything to do with encapsulation or data hiding directly. – scottb Jun 28 '16 at 05:19
  • 1
    *"In Java 8 are we trying to introduce something called partial encapsulation with default and static method?"* ... default methods were a means to an end: a way to evolve legacy Java interfaces without breaking decades worth of existing code. Default methods do introduce more multiple inheritance into Java, specifically multiple inheritance of implementation (or behavior). Java still forbids multiple inheritance of state. – scottb Jun 28 '16 at 05:22

2 Answers2

2

I would say these are additional features on top of what we had before Java 8

Like you said , its correct - there can be a default and static (available to interface only and can not be overridden.)

From oracle ,When you extend an interface that contains a default method, you can do the following

  1. Not mention the default method at all, which lets your extended interface inherit the default method.
  2. Redeclare the default method, which makes it abstract.
  3. Redefine the default method, which overrides it.

So I don't know what to call that (partial encapsulation or not ), they are default implementation and can be overridden. Hiding actual implementation is still an feature here.

Collections in java.utils have used default methods to support lambda expressions . Now some scenarios where it can be helpful.

  1. in a lot of application we need a base implementation class and then we extend that class i.e.BaseImplClass implements IsomeInterface So rather than having a BaseImplClass , you have an option of defining these default implementation in your interface, which can be overrriden if required.

  2. If you have any helper or utility classes , you can use static methods in interface ...which are public,final and can not be overridden

And now if you think of diamond problem which may occur (if a class implements multiple interface with same default method signature , we will have ambiguous situation . And answer is no, you will have error at compilation time in this situation. And you must override that method.

surya
  • 2,581
  • 3
  • 22
  • 34
  • *"in a lot of application we need a base implementation class.... So rather than having a BaseImplClass , you have an option of defining these default implementation in your interface"* ... I'd like to point out that skeletal, base, or abstract implementations of interfaces remain a useful design. The option to replace a skeletal implementation with an interface with default methods exists only if the implementation can be made using only calls to interface methods. If state is required, then an abstract class is still usually the right call. – scottb Jun 28 '16 at 05:30
0

Answer1: I am convinced that default methods provide partial encapsulation. Because when-ever we wrap code inside a class (now possible inside interface) encapsulation is there. Primarily it is a contemporary approach of abstraction, where we can provide what we want to do instead of how we want to do it.

Answer2: It is not a new problem, newbies can do lot danger than this. I think We can setup check-style to not allow such methods.

Gaurava Agarwal
  • 974
  • 1
  • 9
  • 32
  • *"I am convinced that default methods provide partial encapsulation. Because when-ever we wrap code inside a class (now possible inside interface) encapsulation is there. Primarily it is a contemporary approach of abstraction, where we can provide what we want to do instead of how we want to do it."* ... I'm quite sure I have no idea what any of this means. – scottb Jun 28 '16 at 05:32