Overriding object class methods in the Java interface

Consider the following simple code in Java.

package temppkg; interface Interface { @Override public abstract boolean equals(java.lang.Object arg); @Override public abstract String toString(); public void show(); } final class Demo implements Interface { public void show() { System.out.println("Method invoked."); } } final public class Main { public static void main(String...args) { new Demo().show(); } } 

In the code snippet above, an interface named Interface has some object class methods and they contain the @Override annotation, although they are abstract . Now the Demo class has implemented Interface and has not implemented methods equals() and toString(); , the compiler still does not complain and the program runs successfully, why? What is the relationship between interfaces and an object class in Java?

+4
source share
4 answers

The Java language specification makes it clear that interface members are those that are declared in the interface and those that inherit from direct superinterfaces. If the interface does not have a direct superinterface, then the interface implicitly declares a method of public abstract elements corresponding to each public instance of a method declared in the Object class, if only a method with the same signature, the same return type and the offer of compatible throws is explicitly declared by this interface. This is what makes the signature of the Object methods available to the compiler and the code compiles without any errors. Remember, if the interface tries to declare the public instance method declared as "final" in the Object class, this will lead to a compile-time error. For example, 'public final Class getClass ()' is a public instance method declared 'final' in the Object class, and therefore, if the interface tries to declare a method with this signature, then compilation will fail.

http://geekexplains.blogspot.com/2008/06/do-interfaces-really-inherit-from-class.html

+7
source

Check out JLS 9.2 :

If the interface does not have direct superinterfaces, then the interface implicitly declares an open method of abstract members m with signature s returned by type r and a sentence th corresponding to each public instance method m with signature s, return type r and throws declared in Object , if only the method with the same signature, the same return type and compatible throws explicitly declared by the interface. This is a compile-time error if the interface explicitly declares such a method m when m is declared final in Object .

In other words, each interface implicitly defines each of the Object methods, and so you can @Override use these methods. Other methods are not defined in Object , so you cannot override them.

+2
source

In Interface you are not actually redefining anything - an interface, by definition, cannot provide an implementation for any of its methods. The Demo class simply inherits the implementation of equals and toString from Object .

Essentially, an interface in Java contains a set of zero or more method signatures (all of them are implicitly abstract, in your code you made it explicit by adding the abstract keyword), and concrete classes that implement the interface must provide the implementation of these methods. In the case of your code, this implementation comes from Object , since all classes implicitly extend Object , which provides standard implementations for equals and toString (among other methods).

You really shouldn't mark methods in the interface with @Override , as you saw, this is confusing and serves a practical purpose. Instead, use @Override in methods of a particular class that implement interface methods, for example:

 class Demo implements Interface { @Override public void show() { System.out.println("Method invoked."); } } 

In addition, it is not necessary to declare equals and toString in the interface, so you are better off with this definition:

 interface Interface { public void show(); } 
0
source

@Override can only be used for functions defined in the base class Object. The object defines equals and toString , so you can use @Override with them, but not with, say, the show function. It points to the relationship between @Override functions in a class and its base classes, and not with derived classes.

0
source

Source: https://habr.com/ru/post/1389194/


All Articles