• Post Reply Bookmark Topic Watch Topic
  • New Topic

Annotation Programming in java  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am little confused with predefined annotations in Java 5 .

I am confused with using @Override Annotation .

I could not able to understand this below line .

when overriding a method, it helps to prevent errors. If a method marked with @Override fails in correctly overriding the original method in its superclass, the compiler generates an error.



Please tell me what that means actually ??.

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this example


This doesn't compile because test() does not overrides anything
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's explain it with an example....



Every Object has a toString() method which can be overridden. But what if you spell the method wrong? Case it incorrectly?



Technically, there is nothing wrong with this method. As such, it will compile just fine. But it isn't doing the intent, which is to override the toString() method. If you annotate it with @Override the compiler will look for the same method in the super class and if it can't find it, it will throw an error. This way you are notified that something is wrong with your version of the overridden method.

 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably best explained with an example. Assume class B extends class A. Class A has this method:

int doSomething(String str, int i);

Then in class B you decide you want to override the doSomething method but you mistype it:

int doSoemthing(String str, int i);

Without the @Override annotation, class B compiles just fine and then you spend a lot of time trying to figure out why this code isn't working like you think it should:

B b = new B();
int i = b.doSomething("foo", 1) ;

But if you use @Override in class B like this:

@Override
int doSoemthing(String str, int i);

Then the compiler knows you goofed and can generate a error.

(Wow, three responses all typed at the same time!)
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation . Thank you very much Peter ,Gregg and Wouter.

(Atleast i would have spent a hour googling and understanding this concept).

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:Great explanation . Thank you very much Peter ,Gregg and Wouter.

(Atleast i would have spent a hour googling and understanding this concept).



An hour? Really? "use of @Override" in google, first hit explains it just as well as we did.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd consider spending the "hour": at some point you have to stop relying on others to answer all your questions.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An hour? Really?


Ya today my internet will be down till 8 - 9
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!