Java - Inherited Annotation Type

Introduction

The Inherited annotation type is a marker meta-annotation type.

An Inherited annotation type is inherited by a subclass declaration.

Example

In the following code, Ann2 is not annotated with an Inherited meta-annotation, whereas Ann3 is annotated with an Inherited meta-annotation.

@interface Ann2 {
        int id();
}

@Inherited
@interface Ann3 {
        int id();
}

Let's declare two classes, A and B, as follows.

@Ann2(id=1)
@Ann3(id=2)
class A {
}

// Class B inherits Ann3(id=2) annotation from the class A
class B extends A {
}

class B inherits the @Ann3(id=2) annotation from class A because the Ann3 annotation type has been annotated with an Inherited meta-annotation.

Class B does not inherit the @Ann2(id=1) annotation because the Ann2 annotation type is not annotated with an Inherited meta-annotation.