http://www.technicalpage.net/search/label/SQL

>> Annotations in Java



Annotations in Java:

Annotation was introduced in Java 1.5. We are going to discuss about some basic annotations as below :
@Overwrite
@Suppresswarnings
@Deprecated
@FunctionalInterface

@Overwrite --- To achieve method overriding.
Let’s take a case of inheritance between two classes:
public class ClassA {

       public void transaction() {
            
             System.out.println("This is from ParentClass.");
       }
}

public class ClassB extends ClassA {
      
       public void transaction() {
            
             System.out.println("This is from ChildClass.");
       }
}


public class AnnoTation {

       public static void main(String[] args) {
            
             ClassB b = new ClassB();
             b.transaction();
       }
}

Output:
This is from ChildClass.

Above example shows that if you have same methods in parent class and child class, the child method overrides the parent method. So the output shows the result from child class, as shown above, and this is expected.

However, if you do some spelling mistake or typo in writing the method name than the compiler does not notice that typo. As a result of that, the result might be different . Such as , suppose you typed the method name in child class as “transacTion” , then calling “b.transaction();” it will print below result, which is not expected.

Output: This is from ParentClass.

To overcome such mistakes, we use @Overwrite . When you use this annotation, at compile time itself the code shows error because it does not find another method with same name to override and we get the information that the methods in two classes are not with same name. So we go to the method , with which we want to establish the method override , and make the necessary correction.


After correcting the error , it finds method to override and the error goes away, And we get the expected result  as :  This is from ChildClass.



@Suppresswarnings   --- to suppress/disable the compiler warnings.
The warnings can be suppressed at Class or Method or Property.
If applied to a class, all the code inside the class will suppress the given type of warning/s.
If applied to a method, all the code inside the method will suppress the given type of warning/s.
If applied to an individual code, only that code will suppress the given type of warning.

You can use one or more than one  types of suppressWarning arguments like below:
@SuppressWarnings("unused")
@SuppressWarnings ({"unchecked","unused"})






The common suppressWarning arguments are :
unused --- warning for unused code
unchecked --- unchecked operation
all --- suppress all warnings
deprecation --- when the code are deprecated
And these also as per the need : null , cast , divzero , empty , finally , serial etc.

Example of Deprecated SuppressWarning:


@FunctionalInterface --- when this annotation is used, only one method can be used in an interface:

imageFunctionalInterface







No comments:

Post a Comment