Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "must override a superclass method" with @Override?

The following code generates this error message at the public void onClick line.

Multiple markers at this line
- implements android.view.View.OnClickListener.onClick
- The method onClick(View) of type new View.OnClickListener(){} must override a superclass method

I can't understand why. This code is taken from numerous examples I've seen. What can possibly be wrong?

private Button audioButton;  /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      audioButton = (Button) findViewById(R.id.imageButton1);     audioButton.setOnClickListener(new OnClickListener() {         @Override         public void onClick(View button) {             if (button.isSelected()) {                 button.setSelected(false);             }             else {                 button.setSelected(true);             }         }     }); } 
like image 697
Richard Eng Avatar asked Jan 02 '12 04:01

Richard Eng


People also ask

Under what circumstances would a subclass need to override a superclass method?

When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

Is it mandatory to use @override?

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

How do you override the super class method?

When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error.

Can all superclass methods be overridden?

It depends on the type of Superclass, if it's an Abstract class you must override all your method. For concrete class, you only have to override what is required.


1 Answers

Check the project's properties and verify that Java Compiler -> Compiler compliance level is set to 1.6 (or a later version).

It worked for me... i am using eclipse 2021.... and ..

like image 100
Diego Torres Milano Avatar answered Oct 10 '22 12:10

Diego Torres Milano