Javalangannotationinherited Example

Java @Inherited

Introduction to Java @Inherited

The @inherited in Java is an annotation used to mark an annotation to be inherited to subclasses of the annotated class. The @inherited is a built-in annotation, as we know that annotations are like a tag that represents metadata which gives the additional information to the compiler. Same as built-in annotation, which is exits in the Javadoc, it is possible to create another meta-annotation out of existing in the java. There are actually two types of annotations, one type of annotations applied to the java code like @override, and another type of annotations applied to the other annotation like @target @inherited. So @inherited is an annotation that is applied to other annotation whose we want to create subclasses or we want to inherit to make another user define annotation.

Syntax

The syntax of the @inherited in java is –

@Inherited
public @interface MyAnnotation {// code of the MyAnnotation
}
@MyAnnotation
public class SuperClass {
// code of the SuperClass }
public class SubClass extends SuperClass { // code of the SubClass }

As in the above syntax, the class SubClass is inherited from the annotation @MyAnnotation, because it is inherited from SuperClass, and SuperClass has a @MyAnnotation annotation.

How does @Inherited work in Java?

The @Inherited annotation is used or annotated to the annotation (MyAnnotation as in above syntax), which the @interface should prefix. Next, this annotation (MyAnnotation) can be used where ever need to apply as @MyAnnotation. These annotations can be applied just before the declaration of an element and can be applied to any element of the program like variables, class, constructors, methods, etc. When this user-defined annotation is annotated on the superclass, it is automatically inherited to subclasses (subclass as in the above syntax), as we can see in the below examples.

Examples to Implement @Inherited annotation in Java

Next, we write the java code to understand the @Inherited annotation more clearly with the following example where we use @Inherited annotation to inherit in the subclass from the superclass, as below –

Example #1

First, we create an interface for annotation @MyAnnotation, which has two fields, name and code.

Code: // MyAnnotation.java

package demo;
import java.lang.annotation.Inherited;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Inherited
@Target ({ElementType.TYPE, ElementType.METHOD})
@Retention (RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
String name () default "unknown";
String code () default " ";
}

Next, we create a superclass to use the above annotation by annotating any class or method or variable and provide the state name and state code.

Code: //Super.java

package demo;
import demo.MyAnnotation;
@MyAnnotation (name = "Karnataka", code = "KA")
public class Super
{
public String getstateinfo ()
{
return null;
}
}

Next, we use an annotation because it is metadata, which means we should be able to get this metadata or information to use the annotation information when we need it.

Code: //Demo.java

//as sub class
package demo;
import demo.MyAnnotation;
import demo.Super;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
public class Demo extends Super
{
public static void main ( String[] arg ) throws Exception
{
new Super ();
Class <Super>obj = Super.class;
getstateinfo (obj);
Method m = obj.getMethod ("getstateinfo", new Class[]{});
getstateinfo (m);
}
static void getstateinfo (AnnotatedElement e)
{
try
{
System.out.println ("Finding annotations on " + e.getClass ().getName ());
Annotation[] annotations = e.getAnnotations ();
for (Annotation a : annotations)
{
if (a instanceof MyAnnotation)
{
MyAnnotation stateInfo = (MyAnnotation) a;
System.out.println("Name of Annotation :" + stateInfo.annotationType ());
System.out.println("State Name :" + stateInfo.name ());
System.out.println("State code :" + stateInfo.code ());
System.out.println(new Demo ().getClass ().getAnnotation (MyAnnotation.class));
System.out.println(new Super ().getClass ().getAnnotation (MyAnnotation.class));
}
}
} catch (Exception ex)
{
System.out.println( ex );
}
}
}

Output: When we run the Demo.java class, the output is.

Java @Inherited - 1

Explanation: As in the above code, the MyAnnotation annotation is created an also annotated by @Inherited. In the Superclass, the MyAnnotation annotation was using by the statement @MyAnnotation and annotated to the class. And another class Demo is created, which is the subclass of the Superclass because it is extended to Superclass. Farther in the main () method of the Demo class, an object of the Superclass is creating and access its method that is getstateinfo (), through this methoditerating all its annotations and checking whether the annotation is inatnce of MyAnnotation, if yes then printing some of the information as we can see above. But one important thing is that the Demo class or any of its elements not annotated to the MyAnnotation, but it still showing that the MyAnnotation is annotated to this class because it is inherent to the Superclass and Superclass is inherited MyAnnotation.

Next, we rewrite the above java code to understand the @Inherited annotation more clearly with the following example where we will not use @Inherited annotation to annotation MyAnnotation (as annotation created in the above example) to check whether this annotation is inherited in the subclass from its superclass or not, as below –

Example #2

We will keep Super.java and the Demo.java classes the same as above no changes in those classes, but in MyAnnotation.java class, we do little change, just we remove the @Inherited annotation to annotate from the MyAnnotation, as we can see below –

Code: //MyAnnotation.java

package demo;
import java.lang.annotation.Inherited;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// no @Inherited
@Target ({ElementType.TYPE, ElementType.METHOD})
@Retention (RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
String name () default "unknown";
String code () default " ";
}

Output: Next, when we run the Demo.java class, the output is.

Java @Inherited - 2

Explanation: As in the above output, we can see that after state code, the "null" value is printed, that is the return value of the statement "new  Demo ().getClass ().getAnnotation (MyAnnotation.class)", which means that the demo class is not inherited (or annotated) any MyAnnotation annotation from it Superclass, because the @Inherited annotation is not annotated to MyAnnotation to inherit it in the subclass.

Conclusion

The @inherited in java is a built-in annotation applied to another annotation. It is used to marks an annotation to be inherited to subclasses of the annotated class. The @inherited is available in the package java.lang.annotation.Inherited.

Recommended Articles

This is a guide to Java @Inherited. Here we discuss an introduction to Java @Inherited along with the working, appropriate syntax and respective examples to implement. You can also go through our other related articles to learn more –

  1. Java ServerSocket
  2. Java Identifiers
  3. Java Duration
  4. Stack Class in Java

mccarthypurt1937.blogspot.com

Source: https://www.educba.com/java-inherited/

0 Response to "Javalangannotationinherited Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel