boolean equals(Object obj)是Object定义的方法,不算在接口方法内
Topic source楼主给出Oracle官网的解释,为楼主点赞。
对于官网中的这句话: 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.
我尝试给出自己的理解:Comparator接口中的equals方法覆写自父类Object,但其修饰符为Interface默认的public abstract,其他实现此接口的类由于均默认extends Object,所以其不需要重写此方法,而是以Object类中的equals方法作为对Comparator接口equals方法的覆写或实现
- 1
Best of Me
刚开始不是很理解这句话,去查了下FunctionalInterface的定义才知道:
因此,可以理解为Comparator接口的实现类必定会继承Object的equals方法。
顺便补充下,Comparable接口按逻辑上也是一个FunctionalInterface(api和源码都没有注解,故比较疑惑),但是Comparable更倾向是是对象的特性(类似实例方法),而comparator可以充当比较器(类似匿名类对象)。所以在实践中,Comparable一般不用于Lambda表达式。
参考:
https://stackoverflow.com/questions/56827701/is-comparable-interface-a-functionalinterface
https://www.cnblogs.com/black-watch/p/13415890.html#comparable%E6%8E%A5%E5%8F%A3