In Java, the @Inherited
annotation is used to indicate that an annotation should be inherited by subclass methods and fields. This means that if a superclass is annotated with a particular annotation, and the @Inherited
annotation is applied to that annotation, then the subclass will also have that annotation.
Here is an example of how the @Inherited
annotation can be used:
import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Inherited @interface Test { String value() default ""; } @Test class SuperClass { // code goes here } class SubClass extends SuperClass { // code goes here }
In this example, the @Test
annotation is applied to the SuperClass
class, and the @Inherited
annotation is applied to the @Test
annotation. This means that the SubClass
class will also have the @Test
annotation, even though it is not explicitly annotated.
The @Inherited
annotation is only applicable to class-level annotations, and it has no effect on method-level or field-level annotations.