Predefined/Standard Annotations

by | Apr 16, 2021 | Java

Predefined/Standard Annotations

There are mainly seven built-in annotations in JAVA. The various annotations are listed below:

  • Four annotations are imported from the java.lang.annotations package: @Retention, @Documented, @Target, and @Inherited.
  • Another three annotations are included in the java.lang package: @Deprecated, @Overide and @SuppressWarnings.

This article will discuss the various annotations listed above and present a sample code for every type to explain the use of predefined/standard annotations better in actual source codes.

@Deprecated Annotation

  • The deprecated annotation is like a marker annotation. It indicates whether a declaration is obsolete and has been replaced by a newer declaration that is currently active.
  • Whenever you try to deprecate an element, you must use the @deprecated tag.
  • The @deprecated tag is mainly used for documentation purposes, while @Deprecated annotation is mainly used in run-time reflection.
  • @deprecated is of a higher priority as compared to @Deprecated. If both tags are used, @deprecated will be executed by the compiler before @Deprecated.

Example:

public class DeprecatedDemo
{
@Deprecated
public void Display ()
{
System.out.println(“Demo Display”);
}
public static void main (String argsp[])
{
DeprecatedDemo d1 = new DeprecatedDemo();
d1.Display();
}
}

 

OUTPUT

Demo Display

@Overide Annotation

Override Annotations are subsets of Marker Annotations and can only be invoked on methods. An @override annotated method normally overrides a method from its superclass. A compile-time error is lodged in cases where a method cannot override its superclass method. Therefore, this annotation ensures that rather than just overloading, a superclass method is actually overridden.

Example:

class Demo
{
public void Display ()
{
System.out.println (“Base Display () ”);
}
public static void main (String args [])
{
Demo d1 = new Demo();
}
}
class Derived extends Demo
{
@Override
public void Display ()
{
System.out.println ( “Derived Display () ”);
}
]

OUTPUT

Derived Display ()

@SuppressWarnings

When you need to suppress certain compiler warnings, you can use the @SuppressWarnings annotations. The name and string form of the compiler warning should be explicitly suppressed. There is no restriction to the declaration type of this type of annotation.

Warnings are segregated into deprecation warnings and unchecked warnings. Whenever a legacy code interfaces with code that takes the help of generics, an unchecked warning is thrown by the compiler.

Example

class Demo
{
@Deprecated
public void Display ()
{
System.out.println (“Deprecated display ()”);
}
}
public class SuppressWarning
{
@SuppressWarnings ({“checked” , “deprecation”})
public static void main (String args [])
{
Demo d1 = new Demo ();
d1.display ();
}
}

 

OUTPUT

Deprecated display ()

@Documented Annotations

These types of annotations are particularly used when a certain annotation in itself needs to be documented. The use of @documented annotations triggers Javadoc to process the lines of code and add annotations type information in the generated document.

@Target

Target annotations, as the name suggests, targets other annotations. It is designed to act as annotations to other annotations. A constant from the ElementType enumeration is generally taken as an argument by Target annotations.

@Inherited

This type of annotation can only be used on declarations, especially on class declarations. Annotations of this type mainly deal with the concept of inheritance. Therefore, a subclass can easily annotate the annotations of a superclass. If an annotation first checked for in the subclass is not found, the superclass is referred thereafter.

@Retention

Retention mainly deals with the level of the program the annotation is working with. The Retention Policy for the various levels are as follows:

  • SOURCE
  • CLASS
  • RUN-TIME

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Go Coding