articles

Home / DeveloperSection / Articles / Annotations in Java: @Override and @SuppressWarning

Annotations in Java: @Override and @SuppressWarning

Jonas Stuart 2656 28-May-2016

In the previous posts, we have learnt what annotations in java are and why we use these annotations in java program codes. We have learnt the use and demonstration of @Deprecated annotation. Now here we learn the other two very helpful and important annotation called @Override and @SuppressWarning

The @Override Annotation       

The @Override annotation indicates that the annotated method is required to override a method in its superclass. If it does not do so, the compiler will flag an error. We discuss the importance of this annotation after looking at the code illustrated here:

public class OverrideAnnotationDemoApp {

                     public static void main(String[] args) {
                             Cat c = new Cat();
                             c.saySomething();
                     }
}
class Animal {
                     void saySomething() {
                             System.out.println("Animal talking");
                     }
}
class Cat extends Animal {
                     @Override
                     void saySomething() {
                             System.out.println("meow... meow");
                     }
}

 

Here, the Animal class defines one method called saySomething. Cat is a subclass of Animal that redefines the method’s implementation. In the subclass definition, we mark this method with the @Override annotation. When we compile this code, it compiles without any warnings and errors, which is good. Now let’s take out the @Override line from the source and recompile the code.

 It again compiles without any errors. So what’s the use of the @Override annotation? To find out, let’s now modify the method name in the Cat definition to saySomethng—a simple spelling mistake. Now, let’s once again compile the code. The code compiles without errors. However, when we run the code, we see the following output:

Animal talking

What happened here? We were expecting the cat to say “meow… meow.” Such types of errors are hard to find, and programmers have spent many sleepless nights on such trivial-looking errors. Okay, now let’s add the @Override annotation, as before, and recompile the code. Now the compiler throws the following error to the terminal:   

OverrideAnnotationDemoApp.java:22: method does not override or implement a method from a supertype 
@Override
^
1 error

Now, we would probably know that we do not have an overridden method that matches the signature of a method in its superclass. We can easily save ourselves a lot of valuable time by marking those methods we are overriding in a subclass with the @Override annotation.

The @SuppressWarnings Annotation

The use of this annotation tells the compiler not to spit out the specified warnings in its output. To understand its use, let’s go back to our DeprecatedAnnotationDemoApp program. The compilation of this program gives us the method deprecation warning we observed earlier. Now, add the following line before the main function declaration:

@SuppressWarnings({"deprecation"})

public static void main(String[] args) {

Save the file and recompile the code. The deprecation warning error vanishes from the output.We can use this feature judiciously to avoid cluttering the compiler output with unwanted messages.


Updated 22-Jan-2019

Leave Comment

Comments

Liked By