The difference between @override annotation and Java5 of JAVA6

Source: Internet
Author: User

First take a look at the following sample code:

Parent.java

public class Parent...{
  public void parentMethod() ...{
  }
}

Ichild.java

public interface IChild ...{
  void interfaceMethod();
}

Child.java

public class Child extends Parent implements IChild ...{
  @Override
  public void parentMethod() ...{
  }
  @Override
  public void interfaceMethod() ...{
  }
}

Because the code is simple, I don't explain it.

The above code can be compiled by jdk6

In Jdk5, compilation fails, and the output error message is as follows:

Child.java:7: Methods for methods that do not overwrite their parent classes

@Override

^

1 Error

The error message above says that the override annotation on line seventh of Child.java is not correct, because the method (Interfacemethod) is not a parent class method.

Of course, this method is actually a class implementation of the interface method, but the syntax of the jdk6 of the following can be passed, that is, java6 that the class overrides the parent class method and implementation interface method is called override, and Jdk5 do not think so, do not know this is the original Jdk5 bug, Or the original is that the coverage of the parent class method and the implementation of interface method is not the same, unknown. But from the Oo point of view, covering the parent class method and implementing interface methods can be considered override, because their purpose is the same, are for reuse, are a manifestation of polymorphism. This function can be seen from the support of the JDK6.

Almost forgot:

Standard Java API documentation Java5 is the same as Java6 's interpretation of override annotation

JAVA5:

Indicates that ' a method ' declaration is intended to override a, declaration in a. If A is annotated with the annotation type but does not override a superclass method, compilers are required to GE Nerate an error message.

JAVA6:

Indicates that ' a method ' declaration is intended to override a, declaration in a. If A is annotated with the annotation type but does not override a superclass method, compilers are required to GE Nerate an error message.

The two paragraphs are exactly the same, making it unclear that the JAVA6 document does not mention override's support for interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.