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."
Eric Leschinski
source share