Reputation: 26212
Can you override method in java without using annotations? Because eclipse doesn't support it if you use JRE6, you need to switch back to 5 to use @Override
annotation. Will this method be overriden if I remove the annotation?
@Override public String toString() {
return name + " [" + genre + "]";
}
Upvotes: 7
Views: 2090
Reputation: 161022
Can you override method in java without using annotations?
Yes, the @Override
annotation is not required to override a method.
Just by the act of having a method definition with the same method signature of an ancestor class is enough to override a method.
The @Override
annotation is just a reminder to the compiler that says that the method definition is intended to override a method.
If a method that has a @Override
does not actually override a method, the compiler will throw a compiler error -- the annotation serves as an additional compile-time check to see whether the method definition does indeed override a method.
Because eclipse doesn't support it if you use JRE6, you need to switch back to 5 to use @Override annotation.
I happen to use Eclipse targeting Java 6, and am able to use the @Override
annotation, so I'm not quite sure what can be causing this. Annotations has been supported from Java 5, and has been more tightly integrated into the language since Java 6.
Edit
From Andreas_D's comment to the question:
smile - this just indicates, that the current source level for your project is < 1.5. This setting is pretty independant from the JRE, you actually can use JRE 1.6 together with source level 1.4 - and I guess, that's what happened.
This sure seems to be one way a compile error can occur!
The way to check if the "compiler compliance level" is set to something under Java 5 is,
Edit
Regarding to Gandalf StormCrow's screenshot, appears that the compiler compliance level is set lower than JDK 1.5. Clicking on the Change project compliance and JRE to 1.5 quick fix should fix up the current situation.
The issue is, annotations were introduced in Java 5.
It appears that the Java project in Eclipse is currently set so the source code can only contain features which were introduced prior to Java 5, therefore disallowing the use of annotations.
The way to fix the situation is tell Eclipse that features from Java 5 can be used, by setting the compiler compliance level to 1.5 or higher.
Upvotes: 8
Reputation: 37029
You do not need to use the @Override
annotation. However, if you do and the method fails to override a method in a superclass then the compiler will return an error.
Upvotes: 0
Reputation: 5314
@override is just a way of ensuring that if you change the contract (the method overriden) this method will not compile. Without it, it will be considered as a different fonction.
As for the JRE6, i'm not sure what you mean : annotations are supported.
Try the jdk ?
Upvotes: 3