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?
Advertisement
Answer
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