why Interface Default methods?

后端 未结 3 1593
时光取名叫无心
时光取名叫无心 2021-02-07 12:53

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

In ‘the strictest sense’, Default methods are a step backwards beca

3条回答
  •  我在风中等你
    2021-02-07 13:35

    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.

提交回复
热议问题