Does comparator interface inherit Object class?
I think it might, because the Comparator interface contains an equals
开发者_JS百科 method.
From section 9.2 of the Java Language Specification:
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
This allows you to call any of the Object
methods via a reference of an interface type - which makes sense, given that the implementation will certainly be a subclass of Object
.
EDIT: In the case of Comparator
, it so happens that equals
is explicitly redeclared, in order to provide more documentation. However, you'd be able to call equals
on a Comparator
reference anyway.
No interface implements Object class but the implementation does
In Java, every class will ultimately extend Object
. While you can't instanciate Comparator
itself, all comparator implementations will still extend Object
.
The equals()
method is redeclared in Comparator
in order to adapt the Javadoc for the special contract that Comparator
imposes on equals()
No interfaces ever extends/inherit an Object
. Only it's implemented classes does extends Object
implicitly (if not explicitly extended).
The Comparator.equals()
method follows the same signature as Object.equals()
method. The reason for this is stated on the JavaDoc:
However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order.
Interface types are not officially subtypes of Object, but behave as if they were:
- They also implicitly declare all the methods of Object (as quoted by Jon)
- They can be converted to Object by an widening conversion without an explicit cast
- All objects of the interface type are automatically objects of the object type
In this case, the interface type redeclares the equals method in a compatible way, and the implementation from Object is used if the implementing class does not provide its own implementation.
The specification here is done in a way that the default implementation from Object.equals also fits the specification of Comparator.equals, and that every (conformant) implementation of Comparator.equals also fits the specification of Object.equals.
Please refer below link
http://www.docjar.com/html/api/java/util/Comparator.java.html
You can see in the code that Comparator interface has its own equals method.
In java, an interface can never be created by inheriting it from a class. So, no, Comparator
interface doesn't inherit Object
class.
The interfaces are like Roles and its responsibilities are declared as interface methods.
Comparator being the interface just lists all the responsibilities which needs to be provided by a class which implements this interface.
The object of the class which implements Comparator will be subclass of the Object class.
精彩评论