Java Annotation @Inherited

In Java, the @Inherited annotation is used to indicate that an annotation type declared with this annotation is automatically inherited by its subclasses.

This means that if a class or interface is annotated with an @Inherited annotation, its subclasses will automatically inherit that annotation. However, if an interface is annotated with @Inherited, its implementing classes will not inherit the annotation.

It is important to note that the @Inherited annotation only applies to class-level annotations. It does not affect method or field-level annotations. Additionally, if a subclass has its own definition of the same annotation type as its superclass, the subclass's definition will take precedence over the superclass's definition.

Here is an example of an annotation with the @Inherited annotation applied to it:

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    // ...
}
Source‮gi.www:‬iftidea.com

In the example above, MyAnnotation is declared with the @Inherited annotation. If a superclass is annotated with MyAnnotation, any subclasses of that superclass will inherit the MyAnnotation annotation. For example:

@MyAnnotation
public class MyBaseClass {
    // ...
}

public class MySubClass extends MyBaseClass {
    // ...
}

In this example, MySubClass will inherit the MyAnnotation annotation from MyBaseClass due to the @Inherited annotation on MyAnnotation.