69

I am not getting the @Inherited annotation in Java. If it automatically inherits the methods for you then if I need to implement the method in my own way then what about that ?

How does will it come to know my way of implementation ?

Plus it is said if I do not want to use this and do it rather in an old fashioned Java way I have to implement the the equals(), toString(), and the hashCode() methods of the Object class and also the annotation type method of the java.lang.annotation.Annotation class.

Why is that?

I have never implemented those even when I did not know about the @Inherited annotation and the programs used to work fine also .

Please somebody explain me from the scratch about this.

mkobit
  • 43,979
  • 12
  • 156
  • 150
StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103

1 Answers1

133

Just that there is no misunderstanding: You do ask about java.lang.annotation.Inherited. This is a annotation for annotations.It means that subclasses of annotated classes are considered having the same annotation as their superclass.

Example

Consider the following 2 Annotations:

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InheritedAnnotationType {
    
}

and

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface UninheritedAnnotationType {
    
}

If three classes are annotated like this:

@UninheritedAnnotationType
class A {
    
}

@InheritedAnnotationType
class B extends A {
    
}

class C extends B {
    
}

running this code

System.out.println(new A().getClass().getAnnotation(InheritedAnnotationType.class));
System.out.println(new B().getClass().getAnnotation(InheritedAnnotationType.class));
System.out.println(new C().getClass().getAnnotation(InheritedAnnotationType.class));
System.out.println("_________________________________");
System.out.println(new A().getClass().getAnnotation(UninheritedAnnotationType.class));
System.out.println(new B().getClass().getAnnotation(UninheritedAnnotationType.class));
System.out.println(new C().getClass().getAnnotation(UninheritedAnnotationType.class));

will print a result similar to this (depending on the packages of the annotation):

null
@InheritedAnnotationType()
@InheritedAnnotationType()
_________________________________
@UninheritedAnnotationType()
null
null

As you can see UninheritedAnnotationType is not inherited but C inherits annotation InheritedAnnotationType from B.

I don't know what methods have to do with that.

Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
  • 3
    this works great for classes but why not if these are interfaces? just make class A,B, C as interfaces and use C.class.getAnnotation(InheritedAnnotationType.class) and it will not work? – Saurabh Sep 29 '16 at 09:01
  • 5
    @saurabh Indeed this doesn't work for interfaces, see javadoc https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Inherited.html : *"If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type."* (The hierarchy on classes is "a line", if you consider interfaces too, it could become "a tree" resulting in inefficient lookup and/or conflicts.) – fabian Sep 29 '16 at 10:06
  • 3
    We can also simplify the test with `A.class.isAnnotationPresent(InheritedAnnotationType.class)`, which will return a boolean. – Guillaume Husta Jul 20 '17 at 08:47
  • 3
    @saurabh I know this old but because it's still relevant, the docs state: _"Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect."_ – Madbreaks Sep 14 '18 at 15:31