18

Learning java 8 default methods . This link like any other resource on internet says

In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code. But they provide the most elegant and practical way to allow backwards compatibility. It made it much easier for Oracle to update all the Collections classes and for you to retrofit your existing code for Lambda.

My understanding is that java 8 dev/designers provided the default method in interfaces so that all implementing class does not have to unnecessarily override same behavior, hence provide backward compatibility. For example :- if ForEach method would not have been default method, every collection implementing class had to implement it. Agreed .

To overcome that we could have had one class providing implementation of these default methods and then implementing class like arraylist etc could have extended that. This way we could have statisfy both java fundamentals i.e reusability and abstraction i.e keeping the interface pollution less

I am sure java 8 dev/designer have already thought about this as they are much more learned and i am missing something here. Can someone help here so that we developers can also be on top of it as this major change?

asked Nov 15, 2015 at 15:12
1

4 Answers 4

20

Before Java 8, interfaces could have only abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface then its implementation code has to be provided in the class implementing the same interface.

To overcome this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.

The default methods were introduced to provide backward comparability so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods.

answered Oct 8, 2016 at 7:34
Sign up to request clarification or add additional context in comments.

2 Comments

exactly - "for backward compatibility" . As far as we know, one of the developers said that this is exactly what default methods were invented for. But this does not mean that they should be used everywhere in the user code.
I don't know about using them "everywhere" but this form of backwards compatibility could be useful for more than just JDK devs. I don't see a big problem with it as long as you don't go too wild. Apparently they chose to make the feature accessible (and even advertise it) to developers.
14

To overcome that we could have had one class providing implementation of these default methods and then implementing class like arraylist etc could have extended that.

Your suggestion would work only for standard JDK classes (since they usually extends some base classes such as AbstractCollection and AbstractList, were the implementation of the new methods can be added).

What about custom classes that implement JDK interfaces? If, for example, you have a class that implements List but doesn't extend some JDK List implementation, you should be able to switch to Java 8 without having to implement new methods in your class.

With default implementations of new methods in the List interface, you don't have to touch your custom class. You can later add a custom implementation to those methods if you are not satisfied by the default implementation.

answered Nov 15, 2015 at 15:16

4 Comments

In that case that custom class could also extend generic class implementing forEach implementation.But yes again limitation comes if that class already extending some class, my suggestion wont work.
With default implementations of new methods in the List interface, you don't have to touch your custom class. You can later add a custom implementation to those methods if you are not satisfied by the default implementation. Agreed. But also we are moving one step backward in some sense as we are diluting abstraction here in some way
@MSach Even if it didn't extend some class, you would be forced to change its implementation (even if just to add an extends someClass clause).
Agreed with your last comment
2

If there is a requirement to add new method to an interface, clients which use existing interface will be broken as classes needs to implement all methods in the interface.

In this scenario, default and static methods can be used. These methods can have body and clients don't need to implement them, so existing implementations work without any changes.

For example, if you want to enhance interfaces to add methods which accept lambda expressions, you can use default methods.

answered Apr 20, 2018 at 8:48

Comments

0

This is the scenario:

  1. We built an application with several interfaces.

  2. We provided this application to our clients, and they all started using it.

  3. After a while, we decided to update those interfaces. At that time, if we add any new abstract methods, those methods must be implemented by our clients.

  4. In this situation, default and static methods play a crucial role. By adding a new default or static method, our clients are not required to implement them.

General Grievance
5,12039 gold badges39 silver badges60 bronze badges
answered Nov 6, 2024 at 4:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.