简体   繁体   中英

Need for default method implementation in Java 8

While going through the answers for "Need of the defender methods" I came across this answer

Can you please elaborate what does the below mean in the accepted answer?

you should be able to switch to Java 8 without having to implement new methods in your class.

The asker of the original question, M Sach, said that instead of default methods, just put the default implementations in an abstract class.

Eran, the answerer, says that this would break existing code.

For example, suppose you have your own List implementation written in Java 7, which does not inherit from AbstractList . When you migrate your code to Java 8, what happens? There is suddenly a bunch of new methods in the List interface that your own List implementation does not have (the "default" implementation of those methods are in AbstractList , which your class doesn't inherit)! Your code will no longer compile.

This means that when migrating to Java 8, you'd potentially have to write new methods for existing classes.

With default methods however, the default implementation is in the interface itself, so you don't have to write new methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM