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

14
  • 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 ... Commented Jul 12, 2022 at 8:47
  • It's useful to provide a contract for those methods - like "equals must be consistent with compare" Like this Commented Jul 12, 2022 at 8:51
  • @JohannesKuhn not useful at all if the signature is identical to the implementation already present from Object. Commented Jul 12, 2022 at 8:55
  • 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: docs.oracle.com/javase/7/docs/api/java/lang/Override.html Commented Jul 12, 2022 at 9:01
  • 1
    @Stultuske java.util.List has specific requirements for its equals method. Where do you put that requirements? Commented Jul 12, 2022 at 9:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

11 Comments

keeping default methods in mind, interfaces can contain implemented methods
No, Android studio)) I can check there too))
Yeah the same is in IntelliJ idea
@HaykMkrtchyan See the comment I made above to the question. That you can put the Override annotation on any method that is also declared in the Object class is just the defined behavior of the Override annotation. Why they defined it that way is something we can't answer.
@HaykMkrtchyan please, see my updated answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.