When to use: Java 8+ interface default method, vs. abstract method

后端 未结 15 1922
闹比i
闹比i 2020-11-22 07:01

Java 8 allows for default implementation of methods in interfaces called Default Methods.

I am confused between when would I use that sort of interface default

15条回答
  •  野性不改
    2020-11-22 07:39

    when should interface with default methods be used and when should an abstract class be used?

    Backward compatibility: Imagine that your interface is implemented by hundreds of classes, modifying that interface will force all the users to implement the newly added method, even though it could be not essential for many other classes that implements your interface, Plus it allows your interface to be a functional interface

    Facts & Restrictions:

    1-May only be declared within an interface and not within a class or abstract class.

    2-Must provide a body

    3-It is not assumed to be abstract as other normal methods used in an interface.

提交回复
热议问题