Annotations in Java help associate metadata to the program elements such as classes, instance variables, methods, etc. Annotations can also be used to attach metadata to other annotations. These types of annotations are called meta-annotation. Java, by default, does not allow the custom annotations to be inherited. @inherited is a type of meta-annotation used to annotate custom annotations so that the subclass can inherit those custom annotations. The syntax to use @inherited annotation is mentioned below.
@Inherited
@interface CustomAnnotation {
String value () default "GFG";
}
Scenarios:
Below we will be discussing both the scenarios:
- Case 1: Using @Inherited annotation
- Case 2: Without using @inherited Annotation
Now let us discuss both the scenarios and implement the same as a java program to get a better understanding of the same.
Case 1: Using @Inherited annotation
In the code example shown below, we have created a custom annotation named CustomAnnotation and annotated it with @Inherited annotation. We used the CustomAnnotation to annotate the Super class which is Inherited by InheritedAnnotationDemo class. InheritedAnnotationDemo also inherits the CustomAnnotation as it is annotated using @Inherited annotation.
Example
Java
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;
@Inherited
@Target ({ ElementType.TYPE, ElementType.METHOD })
@Retention (RetentionPolicy.RUNTIME)
@interface CustomAnnotation
{
String value() default "GFG" ;
}
@CustomAnnotation (value = "Sky is limitless" )
class Super {
}
public class InheritedAnnotationDemo extends Super {
public static void main(String[] arg) throws Exception
{
System.out.println(
new InheritedAnnotationDemo()
.getClass()
.getAnnotation(CustomAnnotation. class ));
System.out.println(
new Super().getClass().getAnnotation(
CustomAnnotation. class ));
Class obj = Super. class ;
printAnnotationState(obj);
}
static void printAnnotationState(AnnotatedElement ann)
{
Annotation[] annotationsArray
= ann.getAnnotations();
for (Annotation annotation : annotationsArray) {
System.out.println(
"Name of the annotation : "
+ annotation.annotationType());
System.out.println(
"Value : "
+ ((CustomAnnotation)annotation).value());
}
}
}
|
Output
@CustomAnnotation(value="Sky is limitless")
@CustomAnnotation(value="Sky is limitless")
Name of the annotation : interface CustomAnnotation
Value : Sky is limitless
Case 2: Without using @inherited Annotation
In the code example mentioned below everything the same except that we haven’t used @Inherited annotation to annotate our CustomAnnotation. Due to this, the CustomAnnotation is not inherited by the InheritedAnnotationDemo class. So, when we use getAnnotation() method for InheritedAnnotationDemo class it does not return CustomAnnotation. In this case, where we haven’t used any annotation to annotate InheritedAnnotationDemo class it simply returns null.
Example :
Java
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;
@Target ({ ElementType.TYPE, ElementType.METHOD })
@Retention (RetentionPolicy.RUNTIME)
@interface CustomAnnotation
{
String value() default "GFG" ;
}
@CustomAnnotation (value = "Sky is limitless" )
class Super {
}
public class InheritedAnnotationDemo extends Super {
public static void main(String[] arg) throws Exception
{
System.out.println(
new InheritedAnnotationDemo()
.getClass()
.getAnnotation(CustomAnnotation. class ));
System.out.println(
new Super().getClass().getAnnotation(
CustomAnnotation. class ));
Class obj = Super. class ;
printAnnotationState(obj);
}
static void printAnnotationState(AnnotatedElement ann)
{
Annotation[] annotationsArray
= ann.getAnnotations();
for (Annotation annotation : annotationsArray) {
System.out.println(
"Name of the annotation : "
+ annotation.annotationType());
System.out.println(
"Value : "
+ ((CustomAnnotation)annotation).value());
}
}
}
|
Output
null
@CustomAnnotation(value="Sky is limitless")
Name of the annotation : interface CustomAnnotation
Value : Sky is limitless
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Aug, 2022
Like Article
Save Article