Check if derived class have an annotation from super class
annotations, inheritance, java, oop
Solution
If your instance is actually of the subclass type, this is as easy as defining the method in the supertype to have the following implementation:
this.getClass().isAnnotationPresent(annotationToCheck.class);
As the `this` is an instance of DerivedClass, the getClass returns DerivedClass and the check works.
Note2: As @Pshemo mentions in the comments, this only works when the annotation is annotated with `@Retention(RetentionPolicy.RUNTIME)`.
Problem
I need to check, form super class, if derived class have a specific annotation: ``` public class SuperClass { public boolean hasAnnotation() { //super_class_method_code } } @annotationToCheck public class DerivedClass extends SuperClass { //derived_class_code } ``` It is possible in Java? I know how to do in C# but in Java I can't find any solution... :( Thank you!!!