About Those @Override Errors
Since this question comes up a lot on StackOverflow, I figured that I would write up a blog post about it…
If you are trying to import some existing Android code into an
application — such as an Android library project —
and you encounter all sorts of errors about “XXX must override or
implement a supertype method”, try commenting out the
annotation on that method. If the error for that method goes away,
then your real problem is in your Eclipse configuration.
But, first, some history.
Annotations were introduced into Java with Java 1.5. One annotation
in particular that was added was
@Override. In Java 1.5, this
annotation was a hint to the compiler that you were intending to
override a method from a superclass. The hint was so that the compiler
would yell at you if you actually were not overriding a method,
for any number of possible reasons:
- typo in the method name
- wrong parameters
- wrong return type
In Java 1.6, they extended the definition of
@Override to allow it
to be used in cases where you were not technically “overriding” a
superclass method, but you were implementing a method required by an
interface. This usage has proven to be popular, and you will find it
in many bits of Android code.
Eclipse has a “Java compiler compliance” concept, technically independent
of what actual Java compiler you are using for your project builds.
Eclipse enforces that your code meets whatever Java version you declare
in your “compliance” setting. If that setting is set to 1.5, then
Eclipse will complain about
@Override used on methods implemented to
satisfy an interface and not overriding a superclass method.
There are two ways to fix this.
For an individual project, you can go into Project > Properties, choose the “Java Compiler” category on the left, and change the value in the “Compiler compliance level” drop-down. Sometimes, this will be disabled, because you are using your workspace-level setting and are not overriding it at the project level. Sometimes, though, the project might be set to override it at the project level and have it set to 1.5. If you would prefer to not override it on a project where it is overridden, you can uncheck the “Enable Project Specific Settings” checkbox at the top of the dialog.
For your entire workspace, you can go to the Preferences window and choose Java > Compiler from the category tree on the left. Here, you will see another “Compiler compliance level” drop-down, one that controls the default for all of your projects.
Maintaining a consistent 1.6 compiler compliance level will clear up