Concept of functional interface

前端 未结 4 1219
感动是毒
感动是毒 2021-01-06 08:23

When I\'m shooting a glance at lambda expressions, the book touches on a functional interface that has only one abstract method. My issue addresses on that quiz que

4条回答
  •  孤独总比滥情好
    2021-01-06 08:42

    In Java, a method in a subtype overrides a method of a parent type when it has the same signature. Signature means both name and arguments of the method. In particular, arguments must be of the same exact type and must be declared in the same exact order in both methods, i.e. the types of the arguments of the method declared in the subtype can't be subtypes or types wider than the types of the arguments declared in the parent type's method.

    So, in your SmartAdder interface, the method with signature add(double a, double b) does not override the method add(int a, int b) of your Adder interface, because double is wider than int. When a type has two or more methods with the same name but with different arguments, it is called method overloading, and it's totally different than method overriding.

    This is why SmartAdder ends up having two abstract methods, hence it's not a functional interface (which requires the type to have only one abstract method).

提交回复
热议问题