FunctionalInterface注解的类中有多个未实现的方法

背景

今天看面试题:Comparable和Comparator接口的区别,点进Comparator接口看了一下,发现在JDK8中此类加了很多方法,并且使用了@FuncitonnalInterface注解,对此有个疑问——为毛使用了函数式接口的注解,还能有多个方法没有实现?函数式接口不是只允许一个未实现方法吗?
在这里插入图片描述

原因

StackOverflow
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

翻译过来就是,在一个接口中声明的一个抽象方法,如果是Object类中的方法,那么此方法是在接口中不算抽象方法,因为接口都是用来实现的,每个实现类都是Object的子类,之前那个抽象方法相当于自动就被实现了,直接使用的是Object中的方法。

猜你喜欢

转载自blog.csdn.net/chen462488588/article/details/112960268
今日推荐