Java 8 default methods vs. non-abstract methods in abstract classes

前端 未结 5 1075
一个人的身影
一个人的身影 2020-12-16 15:41

Java 8 interface default methods vs. non-abstract methods in abstract classes - are there any differences between the two (besides the differences of iface - class, visibili

5条回答
  •  -上瘾入骨i
    2020-12-16 15:56

    Firstly Default methods allow you to add new methods to interface without breaking existing implementations.

    Also take an example of Collections class which is a utility class for Collection interface right.

    So using default methods we can now move all the utility methods as default implementation in the Collection itself, which will make more sense than making a separate class for such utilities.

    Also you will be able to inherit methods from multiple interfaces, which could not have been done using plain abstract classes.

提交回复
热议问题