·
Define:
@Inherited annotation indicates that the annotation type can be inherited from the super class.
(This is not true by default.)
This is a annotation for annotations.
It means that subclasses of annotated classes are considered having the same annotation as their superclass.
Snippet:
package vv; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class Test { @Inherited @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface VVInherited { } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface VVNone { } @VVNone class A { } @VVInherited class B extends A { } class C extends B { } public static void main(String[] args) { out(new Test().new A().getClass().getAnnotation(VVInherited.class)); out(new Test().new B().getClass().getAnnotation(VVInherited.class)); out(new Test().new C().getClass().getAnnotation(VVInherited.class)); out(""); out(new Test().new A().getClass().getAnnotation(VVNone.class)); out(new Test().new B().getClass().getAnnotation(VVNone.class)); out(new Test().new C().getClass().getAnnotation(VVNone.class)); } static void out(Object o) { System.out.println(o); } }
Prints:
null
@vv.Test$VVInherited()
@vv.Test$VVInherited()
@vv.Test$VVNone()
null
null