Class is not Abstract and does not Override error in Java

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”.

Leave a Comment