Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialyovannyfeliz
917 Points@override?
Why do I have to use "@override"? I know it lets the compiler know I want to override a method? But, can someone give me a bit more detail please?
1 Answer
jcorum
71,830 PointsIt's a safety feature. Without the directive you could end up overriding a method by mistake and not realize it. This way you tell the compiler that what follows is supposed to be an override.
Here's a quote from http://www.javapractices.com/topic/TopicAction.do?Id=223:
The main reason @Override was created was to deal with simple (but nasty) typographical errors. For example, a method mistakenly declared as
public int hashcode(){...}
is in fact not an override - the method name has all lower case letters, so it doesn't exactly match the name of the hashCode() method. It will however compile perfectly well. Such an error is easy to make, and difficult to catch, which is a dangerous combination. Using the @Override annotation prevents you from making such errors.