Class is not Abstract and does not Override error in Java

92,847

Solution 1

How to reproduce that error as simply 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 won't it compile?

Because:

  1. You created a class Cat which implements an interface Animal.
  2. Your interface called Animal has an abstract method called roar which must be overridden.
  3. You didn't provide for method roar. There are many ways to eliminate the compile time error.

Remedy 1, have Cat override the abstract method roar()

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();
}

Remedy 2, change Cat to be an abstract like this:

package javaapplication3;

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

}
interface Animal{
    abstract boolean roar();
}

Which means you can't instantiate Cat anymore.

Remedy 3, have cat stop implementing Animal

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
    }
}
class Cat{ 

}

interface Animal{
    abstract boolean roar();
}

Which makes roar() no longer a contract for things that animals must know how to do.

Remedy 3, extend a class rather than implementing an interface

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 to use depends on what the best model is to represent the problem being represented. The error is there to urge you stop "programming by brownian motion".

Solution 2

Your class implements an interface WiimoteListener, which has a method onClassicControllerRemovedEvent. However, the methods in interfaces are abstract, which means they are essentially just contracts with no implementations. You need to do one of the things here:

  1. Implement this method and all the other methods that this interface declares, which make your class concrete, or
  2. Declare your class abstract, so it cannot be used to instantiate instances, only used as a superclass.

Solution 3

When you implement an Interface you must implement all the methods in that interface. You didn't implement onClassicControllerRemovedEvent.

Solution 4

It appears that WiimoteListener is an interface which defines an onClassicControllerRemovedEvent method. Your class must define all methods that an interface declares or it will not compile without errors.

It may also be that this class was designed using a different version of the WiimoteListener interface (based on an older or newer version of the jar that includes that interface) and that version did not declare the above mentioned method. If so, it may just require building against the version of the jar that your class was made to use.

Share:
92,847
Phil
Author by

Phil

I am in my first year of University studying Computing.

Updated on August 17, 2022

Comments

  • Phil
    Phil over 1 year

    I am getting 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 get a better explanation of what this error means?

  • Phil
    Phil about 11 years
    Ok, thank you, so I will make it concrete, In the library that I am using, how do I know excactly how to implemet this method, I have tried the following: public void onClassicControllerRemovedEvent(ClassicControllerRemovedEven‌​t arg0) { System.out.println(arg0); } but it cannot find the symbol, do I need to delve into the .jar to find the class to implement??
  • zw324
    zw324 about 11 years
    The implementation depends on your purposes, but your implementation should get rid of errors. What symbol does it report cannot find?
  • Phil
    Phil about 11 years
    ClassicControllerRemovedEvent
  • Phil
    Phil about 11 years
    Ok, so what I did was dived into the .jar, and removed any methods that I would ever need to declare, and it seems to be ok; so does this mean that this class is now a concrete class (still going over inheritance in my mind as you may have guessed I am a learner from this question)
  • Alex
    Alex over 3 years
    Thanks a lot the 1st option is wizard