2

As much i know interface provides full abstraction because it can't have any concrete method like abstract class. But from java 8, interfaces can have concrete methods using default keyword and the classes that implement the interface can override these concrete methods. So, does interface really provide full abstraction ?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
SbrTa
  • 148
  • 13
  • 2
    you answered your own question – Tim Apr 30 '18 at 16:01
  • 1
    it doesn't now. – Shanu Gupta Apr 30 '18 at 16:02
  • 1
    Well, in practice, there's not much more to it than what you exactly described. I'm unsure about what you mean by *"fully providing abstraction"* though, and I don't think a SO question is the right format for your interrogation. – ccjmne Apr 30 '18 at 16:04
  • 2
    Just because concrete methods are available does not mean that you have to use concrete methods. So if interface provided full abstraction before the addition then obviously it provides it after the addition. – Richard Chambers Apr 30 '18 at 16:10
  • Exactly that was my question :) In many sources and books, i found that interface provides full abstraction. But then i came to know about default method in java 8. Then question arises in my mind, if there is any other thing that i missed out. @ShanuGupta – SbrTa Apr 30 '18 at 16:11

2 Answers2

2

You still can make an interface provide full abstraction by not writing default methods.

Later, though, there may be a need to add new functionality to this interface and ensure binary compatibility. You would be using default methods as a trade-off between abstraction and flexibility.

Actually, well-designed default methods don't bring many specific (implementation-dependent) things. Have a look at java.util.Comparator. I wouldn't say the methods like thenComparing, naturalOrder break the level of abstraction introduced with the Java 1.2 version.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

Kind of and not, with the introduction of Java8, you can basically use concrete methods in interfaces.

Something like this.

public interface IHelloWorld {
  public default void print() {
    System.out.println("Hello World");
  }
}

Now you can either implement a class or an annonymous class which implements this interface and use print() as if, you would have been writing this method in an abstract class.

Important:

First you cannot create direct variables in your interface. You can overcome this by introducing getters and setters, but that would be bad practice.

Also you cannot set this method final.

Cannot do this:

public final default void print() {
    System.out.println("Hello World");
}

This might seem as a small issue, but in real world this can be a problem. Sometimes you really need a final method to not be changed by children at all.

In Conclusion with Java8 you can also implement defaults in Interfaces, however you cannot set them final and also the more important fact you cannot create class variables, cause it's not a class obviously.

Levent Dag
  • 162
  • 8