Saturday, May 18, 2024
HomeJavaWhy use @Override annotation in Java? Coding Finest Follow Instance

Why use @Override annotation in Java? Coding Finest Follow Instance


Disclosure: This text could include affiliate hyperlinks. While you buy, we could earn a small fee.


public class Particular person{
    non-public String
identify;
 
    public boolean equals(Particular person
particular person){
        return
identify.equals(particular person.identify);
    }
}

right here programmer is trying to override equals() technique,  however as a substitute of overriding, its a overloaded
technique
. This error silently escape from compiler and never even floor on
runtime by any Exception and it’s very laborious to detect.
@Override
annotation in Java prevents this class of mistake. 



In the event you put @Override
annotation above equals technique than compiler will confirm  if this technique really overrides a brilliant class or interface technique or
not. if its not then it throw compilation error like “technique doesn’t
override or implement a technique from a brilliant sort



In brief @Override
annotation saves numerous debugging effort by avoiding this extreme mistake
in Java. This single cause is sufficient to persuade programmers to at all times use
@Override
annotation whereas implementing tremendous sort strategies.




I will provide you with another instance, the place @Override annotation has saved me numerous instances. Generally I make errors like, overriding technique with out argument, which suggests, I intend to override a technique, which takes an argument and finally ends up writing a brand new technique with the identical identify with out argument. 


This turns into actually nasty, particularly if the unique technique is just not summary, as a result of then the compiler won’t present any warning or error. However if you’re utilizing @Override annotation, the Compiler will warn you with an error that it really doesn’t override tremendous class technique.
Other than compile-time checking of overriding, @Override
may also be utilized by IDE and static code analyzer to counsel a default
implementation or coding
greatest practices
.
Why use @Override annotation in Java? Coding Best Practice Example

@Override annotation in Java with Instance

@Override annotation in java best practice codingOne of many main issues with @Override annotation
on JDK 1.5 was that it will probably solely be used along with tremendous class
i.e. compiler throws an error in the event you use
@Override annotation
with interface
technique



From Java 6 onwards you need to use @Override annotation
whereas implementing the interface technique as nicely. This supplies strong compile-time checking
of overriding. If in case you have been utilizing Eclipse
IDE
then you should have confronted difficulty together with 
@Override annotation
the place compiler complains even in the event you override interface technique and solely repair was
both take away all
@Override annotation from the interface technique
or shift to Java supply 1.6 from compiler settings.



It’s best to at all times use @Override annotation at any time when utility, that is even prompt by Google’s Java greatest follow information as nicely. @Override is authorized within the following circumstances :

  1. When a category technique is overriding a super-class technique.
  2. When a category technique is implementing an interface technique.
  3. When an interface technique re specifying a super-interface technique.

The one place, the place you do not need to use @Override is when the dad or mum technique is @Deprecated.




That is all on @Override annotation in Java. It is one in all
the most effective Java coding follow to make use of
@Override annotation
whereas overriding any technique from superclass or interface. This may prevent from making errors that are laborious to seek out and debug with a stay utility. 

This annotation has saved many errors throughout compile time from each junior and senior builders a like. Now days, IDE will mechanically put the override annotation for you everytime you override a superclass technique in Java. 

Different Java greatest practices tutorial from Javarevisited Weblog

20
design sample interview questions for Java builders



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments