Java Annotation Inheritance

11 Feb 2015

I ran into an issue recently with Java, where I had two classes, one extending from the other, and an annotation attached to the super class.

I was using reflection to detect the annotation, but it was returning false! Here is my findings, and my solution to:

How can your subclasses inherit the super classes annotations?

Answer is: java.lang.Inherited

public class Main {
    public static void main(String ... args) {
        A a = new A();
        B b = new B();

class A {}
class B extends A {}

@Inherited // <- here's the key!
@interface Annotation {}

Console Output:


If you take out @Inherited the result would be:

Console Output:


Published on 11 Feb 2015 Find me on Twitter or StackOverflow