1

All classes in java extend the Object class implicitly. But that doesn't concern interfaces. Interfaces can only extend other interfaces, but no classes. However, I can override object class methods inside my interface.

public interface NewInterface {
    @Override
    boolean equals(Object var1);

    @Override
    int hashCode();
}

Can you please explain in simple words how is this even possible? Is there any use case for this?

enter image description here

Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
  • 3
    you don't actually override it, nor does it make any sense to add this in an interface. All it does is force implementing classes to provide an implementation, but since they already inherit one from Object ... – Stultuske Jul 12 '22 at 08:47
  • It's useful to provide a contract for those methods - like "equals must be consistent with compare" Like [this](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html#equals%28java.lang.Object%29) – Johannes Kuhn Jul 12 '22 at 08:51
  • @JohannesKuhn not useful at all if the signature is identical to the implementation already present from Object. – Stultuske Jul 12 '22 at 08:55
  • Ok - how do you place a javadoc comment on an `equals` method then? – Johannes Kuhn Jul 12 '22 at 08:56
  • @Hayk Mkrtchyan : I guess that there's a use case for this approach. If we need to force all the child classes to override equals and hashcode, that is a good way of doing it ==> no, since you are not forcing all the child classes to override them. You are merely forcing them to provide an implementation, whether it's a new one, or the one they inherited from Object – Stultuske Jul 12 '22 at 08:58
  • @JohannesKuhn not in an interface, that's for sure. If needed, I would add it in the implementing class itself. – Stultuske Jul 12 '22 at 08:59
  • 1
    If you are wondering why you can put the @Override annotation on those methods, the answer is in the documentation of the Override annotation: *If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold: (...) The method has a signature that is override-equivalent to that of any public method declared in Object.* See: https://docs.oracle.com/javase/7/docs/api/java/lang/Override.html – OH GOD SPIDERS Jul 12 '22 at 09:01
  • @Stultuske - yeah you're right. I was in kotln, there was a little different)) – Hayk Mkrtchyan Jul 12 '22 at 09:02
  • 1
    @Stultuske `java.util.List` has specific requirements for its `equals` method. Where do you put that requirements? – Johannes Kuhn Jul 12 '22 at 09:10
  • Somewhat related - [Does an interface by default extend object](https://stackoverflow.com/questions/13776018/does-an-interface-by-default-extend-object). I think this question is a useful read on the topic. – Chaosfire Jul 12 '22 at 09:19
  • @JohannesKuhn honestly, I wouldn't have put it on List. Personally, I wouldn't have mind there being an implementation of List where the order of the List was irrelevant for comparison purposes. – Stultuske Jul 12 '22 at 09:35
  • @Stultuske The requirement is that you can compare different `List`s (doesn't matter if `ArrayList`, `LinkedList`,...) with each other. And they have to follow the general contract of `Object.equals()` - that means, `list1.equals(list2) == list2.equals(list1)`. How do you guarantee that without putting it on the interface? – Johannes Kuhn Jul 12 '22 at 09:39
  • @JohannesKuhn again: had I designed it, I wouldn't have. Either way, just because it is in the javadoc, how does that prevent some wacko to create his implementation that just uses the default equals, or an other preference implementation? It doesn't, neither. – Stultuske Jul 12 '22 at 09:43
  • By now having a good argument "you did violate the contract - it's your fault". It's part of the contract. Has to be followed. You can't prevent some wacko from doing wacko things. – Johannes Kuhn Jul 12 '22 at 09:50

1 Answers1

2

Interface is a just contract. It says that all classes that inherits interface should implement these methods. Interface cannot have implementation. It is possible to override a class that implements this interface.

However, from Java 8 you can define static methods in interfaces in addition to default methods.

UPDATE:

The members of an interface are:

  • Those members declared in the interface.
  • Those members inherited from direct superinterfaces.
  • If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in Object, . 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. Now it is clear that all superinterface have abstract member method corresponding to each public instance method declared in Object .

Read more about interface members here

StepUp
  • 36,391
  • 15
  • 88
  • 148