3

Quick question. I am using Eclipse and I am getting the The method must override or implement a superclass method error, except Eclipse is using compliance of Java 1.7.
Here is my Code:

public abstract class M4 implements Armory {

@Override
public Integer weaponAmmo(int wepAmmo) {
    wepAmmo = 10;
    return wepAmmo;
}

@Override
public Integer weaponDamage(int wepDamage) {
    wepDamage = 2;
    return wepDamage;
}

@Override
public String weaponName(String wepName) {
    wepName = "M4";
    return wepName;
}

And here is the interface Code:

public interface Armory {
        public Integer weaponAmmo(int wepAmmo);
        public Integer weaponDamage(int wepDamage);
        public String weaponName(String wepName);

    }

Any Ideas?

1

4 Answers 4

6

You don't need to use @override annotation in your method implementation as you are not overriding the methods. You are just implementing the interface methods. This annotation is required when your override any super class methods.

Remove the @Override annotations and it should be fine.

4
  • Question is why its not asking for java 1.7 in eclipse
    – someone
    Commented Dec 23, 2012 at 5:50
  • Ah, Got it! For some reason whenever I saved, eclipse would stick those back in as if I needed them. I turned that off and deleted the annotations so It's all good now, thanks! Commented Dec 23, 2012 at 5:54
  • @ShandanSpencer If you think it's helpful then don't forget to accept the answer. Commented Dec 23, 2012 at 5:56
  • 1
    @ShandanSpencer I would suggest you to get them (annotations) back. These annotations give the compiler more control over your code during refactoring. Commented Dec 23, 2012 at 6:27
2

In Java 5 @Override was allowed only for methods overriding super class methods. Since Java 6 @Override is also allowed for methods implementing interface methods.

1

You are not overriding anything, you are implementing them . So i guess @Override can be removed.

EDIT: I think your compiler is set to Java 1.5 in eclipse

Why does Eclipse complain about @Override on interface methods?

@override on interface implementations are allowed from java 1.6 onwards.

0

Even if Eclipse has been set to compliance level higher than Java 1.5 this error may occur because of Project properties as in your case. Right click on your Project > Properties > Java Compiler. Change it to 1.6 or higher here too. Do Project > Clean.

Not the answer you're looking for? Browse other questions tagged or ask your own question.