Whether the subclass inherits the annotation-inherited of the parent class

Source: Internet
Author: User

Search for such an article on the Internet. The content is as follows:

--------------- The post starts ----------------

 

When you define the annotation type and use it on the code, the annotation in the default parent class will not be inherited to the sub-class. You can add Java when defining annotation. lang. annotation. the annotation of inherited, which allows your defined annotation type to be inherited.

For example:

  • Debug. Java
  1. Package onlyfun. Caterpillar;
  2. Import java. Lang. annotation. retention;
  3. Import java. Lang. annotation. retentionpolicy;
  4. Import java. lang. annotation. Inherited;
  5. @ Retention (RetentionPolicy. RUNTIME)
  6. @ Inherited
  7. Public @ interface Debug {
  8. String value ();
  9. String name ();
  10. }

Use it in the following program:

  • SomeObject. java
  1. Package onlyfun. caterpillar;
  2. Public class SomeObject {
  3. @ Debug (
  4. Value = "unit ",
  5. Name = "debug1"
  6. )
  7. Public void doSomething (){
  8. //....
  9. }
  10. }

If you have a category inherited from someobject, @ debug will also be inherited. However, inherited does not work in Sun JDK 5.0.

--------------- Post end ----------------

 

The author did not carefully read the JDK documentation when writing this article,

 

See the original JDK document:

  1. Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.

Meaning:

Note: If you use the annotation type to comment anything other than the class, the meta annotation type is invalid. Note that this metadatabase only facilitates the inheritance of comments from superclasses; the comments for Implemented interfaces are invalid.

 

That is to say, the annotation inheritance can only be for class annotations, and none of the others works.

 

Tested by myself,If the subclass only inherits the parent class and does not overwrite (Override) the corresponding method using the inherited annotation, The subclass will still inherit the annotation of the method corresponding to the parent class;Otherwise, if the child class overwrites the method, the corresponding annotation will also be overwritten, that is, the annotation of the parent class method will not be inherited by the quilt class method.

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.