-3

i wonder what does the @Override does in Java? My program runs properly without it. Netbeans put a yellow line in my method suggesting to use @Override. But even if i ignore the suggestion, my program runs without any error. Well, i'm just curious. thanks for any reply.

deo toh
  • 15
  • 1
  • 7
  • Please do a google search before posting on SO. Here's a description about @Override: http://docs.oracle.com/javase/7/docs/api/java/lang/Override.html – Aritra Sep 30 '14 at 12:52
  • thanks for the suggestion. i'll see to it that i check google first before asking. – deo toh Sep 30 '14 at 13:11

3 Answers3

2

It tells indicates to the compiler that the annotated method is overriden, either from a superclass or an interface. Your code will compile without it, but in the event that a method that doesn't override anything happens to be using that annotation, the code will fail to compile.

Resource:

kolossus
  • 20,559
  • 3
  • 52
  • 104
0

It is useful for the situation where you think you are Overriding a method but accidentally used the wrong signature. For example lets say you need to override a class's method that looks like this:

public String getProperty(int x, int y) {...}

But you accidentally implement:

public String getProperty(int x){...}

You would not get a compile error for this. But if you include the @Override annotation on your implementation of that method, then the code will not compile and your IDE will let you know. So its mainly for safety.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
0

Although it complies ok, it's always a good coding practice to use the annotation when you purposely override a method. This will help in avoiding typo's that makes an overridden method looking like just another method.

devC
  • 1,384
  • 5
  • 32
  • 56