why Interface Default methods?

后端 未结 3 1596
时光取名叫无心
时光取名叫无心 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:37

    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.

提交回复
热议问题