Why is it not necessary to override both methods of interface Comparator in Java

前端 未结 3 516
别那么骄傲
别那么骄傲 2021-02-09 22:17

We know that it is necessary to implement all methods of an interface, if we want to make an object of that class. But why is it not necessary to implement both the methods

3条回答
  •  误落风尘
    2021-02-09 23:06

    Since all classes implicitly extend Object every implementation of a Comparator has an equals method, because every Object has one.

    It would be the same if you define an interface with a toString() method.

     public interface ToString {
          public String toString();
     }
    
     public class SomeClass implements ToString {
         // toString implicitly implemented, because Object defines it
     }
    

    When you look at the class it says "implements ToString" and this is true, isn't it?

提交回复
热议问题