public class JavaApplication33 { public static void main(String[] args) { T t = new T(); t.test(); t.test2(); } } public interface Test { void test(); void test2(int a); } public class T implements Test { @Override public void test() { System.out.println("test"); } @Override public void test2() { System.out.println("test2"); } } 

Can you please tell me what is the error? I watch a lesson on YouTube about the interfaces, everything works for the author.

The error itself:

Error: java: method doesn’t override or implement a method from a supertype
Error: java: com.company.T is abstract; cannot be instantiated

And another such question: I read somewhere that methods of interfaces cannot be overwritten - I don’t know, or I read it crookedly, but why does he do just that?

  • 3
    Instead of public void test2() { should be public void test2(int a) { - апр
  • one
    The interface defines test2 with an integer argument. You tried to implement test2 without arguments. There was no such option in the interface, therefore the compiler produced an error. - ߊߚߤߘ
  • one
    Without the @Override directives, the java file would have been built without warning, but the resulting code would have worked incorrectly. - ߊߚߤߘ
  • 3
    Not so, Существующую сигнатуру никто не определил, поэтому... And on the signature without an argument to the compiler, don't care, this is another method. - vp_arth
  • 2
    @Arhad if there were no @Override annotations, the class T would still not compile: "error: it is not abstract and does not override the abstract method test2 (int) in Test" . - Regent

2 answers 2

If you read javadoc for the @Override annotation, you will see the following:

Indicates that a method declaration is intended to override a method declaration in a supertype. The following conditions hold:
- The method doesn’t override or implement a method.
- The method has a signature that is an object.

Those. This annotation is necessary in order for the compilation to check whether the method is redefined or not. Those. It checks whether a method with a similar signature is defined in the parent class or interfaces that the class implements.

In your case, the error was in the wrong implementation of the interface. You have not correctly declared the signature for the test2 method (in the interface with parameters, not in the class). Annotation, nothing to do with.

    Annotation @Override carries a double meaning:

    1. This is a direct indication to the compiler that this method overlaps the method declared in the parent class and / or interface. If, say, such an annotation is not specified, and the method signature is set incorrectly, the compiler will not give an error - which can lead to fatal consequences.
    2. This is a reminder to the developer that this method appeared for a reason, by itself, and overrides the method declared in the superclass and / or interface - at least I most often use the @Override annotation in this key.

    Further, before Java 6 — the @Override annotation worked only for methods of the superclass, starting with Java 6 — the @Override annotation can (and should) be applied to the methods declared in the interface. Traces of this dualism still remain in the settings of some IDEs, where you can specify the scope of the @Override annotation:

    enter image description here