0

I was studying lambada and there was a point which states that in java 8 we can declare a method with definition in interfaces like

interface Test {
                default String method(){
                        return "string";
                                       }
               }

and as per specification we can use two methods with same signature but depends on programmer how he wants to use it? Now the question is same task can be if achieved by using definition not declaration then what's the point of using default method?

like they behave same as regular method definition and programmer need to declare body and rest part?

what is the actual point as it seems a bit hard to grasp

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
bananas
  • 1,176
  • 2
  • 19
  • 39
  • You **may** override the default method when you *implement* the `interface`, but you don't have to. – Elliott Frisch May 20 '16 at 04:51
  • the point is `if it has same behavior like interface without default methods` then why it is in **java8** update?? – bananas May 20 '16 at 04:54
  • there must be something under the hood – bananas May 20 '16 at 04:55
  • 1
    Because before Java 8, you **couldn't** add a default method; and thus you couldn't add an implementation to an `interface` definition. – Elliott Frisch May 20 '16 at 04:56
  • @ElliottFrisch can u provide me a good link on `implementation to an interface` part,that will be great – bananas May 20 '16 at 05:00
  • 1
    Default was mainly introduced for backwards compatibility. If you add a new method to an interface every implementing class has to be adapted. Using a default method you don't have to adapt implementing classes but still can add new behaviour that can be overriden. – Kagemusha May 20 '16 at 05:00
  • Before Java 8, an interface could only have something like `String method();` In Java 8+ you can provide a *method body* if you also make it `default`. – Elliott Frisch May 20 '16 at 05:05
  • i got a answer and I'm posting it please check and tell me if i don't miss anythng – bananas May 20 '16 at 05:06
  • 1
    Try Googling [`java default method explained`](https://www.google.com/search?q=java+default+method+explained) and you'll find lots of articles explaining this. – Andreas May 20 '16 at 05:12
  • 1
    Have a look at this blog http://muhammadkhojaye.blogspot.in/2014/03/interface-default-methods-in-java-8.html – afzalex May 20 '16 at 05:17
  • Are you asking more about the [purpose of Default or Defender methods in Java 8](http://stackoverflow.com/q/19998309/2711488)? – Holger May 23 '16 at 13:59

1 Answers1

0

thanks @ElliottFrisch and @kagemusha for hint after searching i got the answer

Why default methods?

List<?> list = …
list.forEach(…); // lambda code goes here

The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.

So it’d be really frustrating if we have lambdas in Java 8 but couldn’t use those with the standard collections library since backwards compatibility can’t be sacrificed.

Due to the problem described above a new concept was introduced. Virtual extension methods, or, as they are often called, defender methods, can now be added to interfaces providing a default implementation of the declared behavior.

Simply speaking, interfaces in Java can now implement methods. The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.

It doesn’t seem to be the language feature that would be appropriate to use every day, but it seems to be essential for Java Collections API update to be able to use lambdas naturally.

bananas
  • 1,176
  • 2
  • 19
  • 39