The class is not abstract and does not cancel the error in Java

I get a compile-time error with Java:

MyClass is not abstract and does not override abstract method onClassicControllerRemovedEvent( wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent) in wiiusejevents.utils.WiimoteListener) 

Here is the class:

 import wiiusej.WiiUseApiManager; import wiiusej.Wiimote; import wiiusej.wiiusejevents.physicalevents.ExpansionEvent; import wiiusej.wiiusejevents.physicalevents.IREvent; import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent; import wiiusej.wiiusejevents.utils.WiimoteListener; import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent; import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent; import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent; import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent; public class MyClass implements WiimoteListener{ public void onButtonsEvent(WiimoteButtonsEvent arg0) { System.out.println(arg0); if (arg0.isButtonAPressed()){ WiiUseApiManager.shutdown(); } } public void onIrEvent(IREvent arg0) { System.out.println(arg0); } public void onMotionSensingEvent(MotionSensingEvent arg0) { System.out.println(arg0); } public void onExpansionEvent(ExpansionEvent arg0) { System.out.println(arg0); } public void onStatusEvent(StatusEvent arg0) { System.out.println(arg0); } public void onDisconnectionEvent(DisconnectionEvent arg0) { System.out.println(arg0); } public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) { System.out.println(arg0); } public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) { System.out.println(arg0); } public static void main(String[] args) { Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true); Wiimote wiimote = wiimotes[0]; wiimote.activateIRTRacking(); wiimote.activateMotionSensing(); wiimote.addWiiMoteEventListeners(new MyClass()); } } 

Can I better explain what this error means?

+7
source share
4 answers

Your class implements the WiimoteListener interface, which has the onClassicControllerRemovedEvent method. However, the methods in the interfaces are abstract , which means that they are essentially contracts without implementations. You need to do one of the following:

  • Implement this method and all other methods declared by this interface that make your class concrete, or
  • Declare an abstract class, so you cannot use it to instantiate an instance that is used only as a superclass.
+7
source

When implementing an interface, you must implement all the methods in this interface. You have not implemented onClassicControllerRemovedEvent .

+3
source

How to reproduce this error as simple as possible:

Java Code:

 package javaapplication3; public class JavaApplication3 { public static void main(String[] args) { } } class Cat implements Animal{ } interface Animal{ abstract boolean roar(); } 

Shows this compile time error:

 Cat is not abstract and does not override abstract method roar() in Animal 

Why doesn't it compile?

Because:

  • You have created a Cat class that implements the Animal interface.
  • Your Animal interface has an abstract method called roar, which needs to be redefined.
  • You did not specify a roar method. There are many ways to resolve compile-time errors.

Eliminate 1, ask Cat to override the abstract bellowing method ()

 package javaapplication3; public class JavaApplication3 { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.roar()); } } class Cat implements Animal{ public boolean roar(){ return true; } } interface Animal{ abstract boolean roar(); } 

Solution 2, change Cat as abstract:

 package javaapplication3; public class JavaApplication3 { public static void main(String[] args) { Cat c; } } abstract class Cat implements Animal{ } interface Animal{ abstract boolean roar(); } 

This means that you can no longer copy Cat.

Elimination 3, Cat Stop Has Animal

 package javaapplication3; public class JavaApplication3 { public static void main(String[] args) { Cat c = new Cat(); } } class Cat{ } interface Animal{ abstract boolean roar(); } 

What makes roar () no longer a contract is that animals need to know how to do it.

Solution 3, class extension, not interface implementation

 package javaapplication3; public class JavaApplication3 { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.roar()); } } class Cat extends Animal{ } class Animal{ boolean roar(){ return true; } } 

The remedy for use depends on which best model represents the problem being presented. The mistake is to convince you to stop "programming with the brown movement."

+3
source

WiimoteListener to be the interface that defines the onClassicControllerRemovedEvent method. Your class must define all the methods declared by the interface, or they will not compile without errors.

It may also be that this class was developed using a different version of the WiimoteListener interface (based on an older or newer version of the jar that includes this interface), and this version did not declare the above method. If so, it may only require building against the version of the jar that your class should have used.

+2
source

All Articles