I have a parent class Parent
and a child class Child
, defined thus:
class Parent {
@MyAnnotation("hello")
void foo() {
// implementation irrelevant
}
}
class Child extends Parent {
@Override
foo() {
// implementation irrelevant
}
}
If I obtain a Method
reference to Child::foo
, will childFoo.getAnnotation(MyAnnotation.class)
give me @MyAnnotation
? Or will it be null
?
I'm interested more generally in how or whether annotation works with Java inheritance.
Copied verbatim from http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html#annotation-inheritance:
Annotation Inheritance
It is important to understand the rules relating to inheritance of annotations, as these have a bearing on join point matching based on the presence or absence of annotations.
By default annotations are not inherited. Given the following program
@MyAnnotation
class Super {
@Oneway public void foo() {}
}
class Sub extends Super {
public void foo() {}
}
Then
Sub
does not have theMyAnnotation
annotation, andSub.foo()
is not an@Oneway
method, despite the fact that it overridesSuper.foo()
which is.If an annotation type has the meta-annotation
@Inherited
then an annotation of that type on a class will cause the annotation to be inherited by sub-classes. So, in the example above, if theMyAnnotation
type had the@Inherited
attribute, thenSub
would have theMyAnnotation
annotation.
@Inherited
annotations are not inherited when used to annotate anything other than a type. A type that implements one or more interfaces never inherits any annotations from the interfaces it implements.