Question
Why Eclipse Says “Must Override a Superclass Method” in Java Android Projects
Question
In Eclipse, after re-importing a Java Android project, many overridden methods start showing this error:
The method must override a superclass method
This often happens in anonymous inner classes and listener implementations. For example:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu arg1, View arg2,
ContextMenuInfo arg3) {
}
});
I expected this method to correctly override the interface method. Sometimes Eclipse originally generates the same method with placeholder parameter names such as arg1, arg2, and arg3, but the override error still appears.
A version with clearer parameter names would look like this:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
}
});
Why does Eclipse report that the method does not override a superclass or interface method after importing the project, and how can this be fixed without manually recreating every overridden method?
Short Answer
By the end of this page, you will understand what the Java override error really means, why parameter names are usually not the cause, and how Eclipse/Android project configuration problems can make valid overridden methods appear broken after import. You will also learn how to diagnose signature mismatches, missing libraries, wrong Java version settings, and Android SDK issues.
Concept
In Java, a method overrides another method only when its method signature matches exactly with a method from a superclass or an implemented interface.
For an override to be valid, Java checks things like:
- method name
- parameter types
- parameter order
- return type compatibility
- access level rules
- whether the parent/interface method is actually visible in the current project setup
A very important beginner point: parameter names do not matter for overriding.
These two methods are equivalent as far as overriding is concerned:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
and
public void onCreateContextMenu(ContextMenu arg1, View arg2, ContextMenuInfo arg3)
Java only cares about the types: ContextMenu, View, and ContextMenuInfo.
So if Eclipse says:
The method must override a superclass method
then the real issue is usually one of these:
Mental Model
Think of overriding like replacing a specific key in a lock.
- The method name is the key shape.
- The parameter types and order are the exact grooves on the key.
- The parent class or interface is the lock.
If the lock is missing, damaged, or replaced with a different one, even a correct key seems wrong.
That is what often happens after importing a project into Eclipse:
- your code still looks correct
- but Eclipse is no longer seeing the same parent interface or Android SDK classes
- so it behaves as if your method does not match anything
The variable names like menu or arg1 are just labels written on the key. They do not affect whether the key fits the lock.
Syntax and Examples
Basic override syntax in Java
class Parent {
public void greet(String name) {
System.out.println("Hello " + name);
}
}
class Child extends Parent {
@Override
public void greet(String name) {
System.out.println("Hi " + name);
}
}
This works because:
- method name matches:
greet - parameter list matches:
(String) - return type matches:
void
Parameter names do not affect overriding
class Parent {
public void greet(String name) {}
}
class Child extends Parent {
@Override
public {}
}
Step by Step Execution
Trace a small override example
Consider this code:
interface ClickHandler {
void handle(String text);
}
class MyHandler implements ClickHandler {
@Override
public void handle(String arg1) {
System.out.println(arg1);
}
}
What happens step by step
-
Java reads the interface:
void handle(String text);It records that any implementing class must provide a method named
handlewith oneStringparameter. -
Java reads the class:
class MyHandler implements ClickHandlerNow it expects
MyHandlerto implement that method.
Real World Use Cases
Overriding is used constantly in real Java and Android development.
Android UI callbacks
You override listener methods such as:
onClick(...)onCreateContextMenu(...)onItemSelected(...)onCreate(...)in activities
Framework lifecycle methods
Frameworks call your code through known method names and signatures. For example:
- Android activity lifecycle methods
- servlet methods in Java web apps
- test methods in test frameworks
Custom class behavior
You override parent methods to change how an object behaves:
@Override
public String toString() {
return "User object";
}
Event-driven programming
Listeners and callbacks depend on exact method signatures. If the signature is wrong, the framework will not call your code correctly, or the compiler will reject it.
IDE-assisted development
IDEs like Eclipse generate override methods for you. But if project metadata, SDKs, or build paths are wrong, generated code may no longer line up with what the compiler expects.
Real Codebase Usage
In real projects, developers rely on a few patterns to make override-related issues easier to catch and fix.
Always use @Override
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
}
Why:
- catches mistakes immediately
- prevents silent signature mismatches
- makes intent clear to other developers
Let the IDE generate methods
In Eclipse or other IDEs, generating methods from the interface/class is safer than typing them manually. It reduces mistakes in:
- parameter types
- nested class names
- checked exceptions
- return types
Fix project configuration first
In imported projects, developers usually check these before touching code:
- build path
- JRE system library
- Android SDK target
- project facets or nature
- compiler compliance level
Prefer fully qualified understanding of nested types
In Android, ContextMenuInfo may actually be a nested type:
ContextMenu.ContextMenuInfo
If imports are broken or ambiguous, understanding the full type helps diagnose the problem.
Common Mistakes
1. Thinking parameter names affect overriding
Broken assumption:
@Override
public void onCreateContextMenu(ContextMenu arg1, View arg2,
ContextMenu.ContextMenuInfo arg3) {
}
This is not broken because of the names. arg1, arg2, and arg3 are fine.
How to avoid it
Focus on:
- parameter types
- method name
- return type
- correct parent/interface
2. Using the wrong parameter type
Broken:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, Object menuInfo) {
}
Correct:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
}
How to avoid it
Comparisons
Related things that are easy to confuse
| Concept | What it means | What matters | Common confusion |
|---|---|---|---|
| Overriding | Replacing a parent/interface method in a subclass or implementation | Same signature | People think parameter names matter |
| Overloading | Same method name, different parameter list in the same class | Different parameters | Often mistaken for overriding |
| Implementing an interface method | Providing the required method from an interface | Exact required signature | Similar to overriding in practice |
@Override annotation | Compiler check that a method really overrides something | Correct parent method must exist | Seen as the cause of the error, but it only exposes it |
Overriding vs overloading example
{
{}
{}
}
Cheat Sheet
Quick rules for Java overriding
- Method name must match
- Parameter types and order must match
- Parameter names do not matter
- Return type must be compatible
- Access cannot be more restrictive than the parent method
@Overridehelps detect mistakes
This does matter
@Override
public void run(String name) {}
This does not matter
@Override
public void run(String arg1) {}
For Eclipse import issues, check these first
- Is the correct Android SDK configured?
- Is the project build path valid?
- Is the correct API level attached?
- Are imports correct?
- Is the compiler compliance level correct?
- Does Eclipse recognize the project as an Android/Java project properly?
Typical Android fix checklist
- Refresh the project
- Clean the project
- Check
Properties -> Java Build Path - Check Android SDK/project target
- Verify imports
- Regenerate overridden methods if needed
- Rebuild
FAQ
Why does Eclipse say “must override a superclass method” when the method looks correct?
Usually because Eclipse cannot resolve the correct parent class or interface, or the method signature differs slightly from the required one.
Do parameter names like arg1 and arg2 break overriding in Java?
No. Parameter names do not affect method overriding. Only the parameter types and order matter.
Why does this happen after importing an Android project into Eclipse?
Importing can break SDK references, build paths, compiler settings, or project metadata. Then Eclipse may no longer recognize the correct Android interfaces.
Should I remove @Override to fix the error?
No. @Override is useful because it reveals the real problem. Removing it may hide a bad signature or broken configuration.
How can I quickly fix many override errors at once?
Start by fixing project setup: Android SDK, Java build path, compiler level, and imports. If the environment is correct, many errors disappear without rewriting methods.
Is this a formatting issue caused by methods inside listeners or anonymous classes?
Usually no. Anonymous inner classes are valid. The problem is normally the resolved types or project configuration, not indentation or formatting.
Can the wrong import cause an override error?
Yes. If Eclipse imports a similarly named class from the wrong package, the signature no longer matches the expected parent method.
Mini Project
Description
Build a small Java example that demonstrates valid and invalid overriding, then use it to practice diagnosing the exact reason an @Override error appears. This project is useful because it separates real signature problems from IDE configuration confusion.
Goal
Create a small program that shows one correct override and one incorrect override, then fix the incorrect version.
Requirements
- Create one interface with a single method.
- Create one class that correctly implements the interface.
- Create another class with a broken method signature using
@Override. - Fix the broken signature so the code compiles.
- Observe that changing parameter names alone does not break overriding.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.