问题
Error :
java:com.company.Bicycle is not abstract and does not override abstract method applyBreakes(int) in com.company.Vehicle and same for the Bike class.
package com.company;
interface Vehicle {
//all are the abstract method.
void changeGear(int a);
void speedUp(int a);
void applyBreakes(int a);
}
class Bicycle implements Vehicle {
int speed;
int gear;
//to change speed
@Override
public void changeGear(int newGear) {
gear = newGear;
}
//to increase speed
@Override
public void speedUp(int increment) {
speed = speed + increment;
}
//to decrease speed
public void applyBreaks(int decrement) {
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed + " gear: " + gear);
}
}
class Bike implements Vehicle {
int speed;
int gear;
//to change gear
@Override
public void changeGear(int newGear) {
gear = newGear;
}
//to increase speed
@Override
public void speedUp(int increment) {
speed = speed + increment;
}
//to decrease speed
public void applyBreaks(int decrement) {
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed + " gear: " + gear);
}
}
class GFG {
public static void main(String[] args) {
//creating an instance of Bicycle
//doing some operations
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBreaks(1);
System.out.println("Bike present state :");
bicycle.printStates();
//creating instance of bike.
Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBreaks(3);
System.out.println("Bike present state :");
bike.printStates();
}
}
回答1:
Because you misspelled the method name in the interface (and/or in the implementation).
Interface method name is applyBreakes, while the implementations call it applyBreaks.
So the classes don't implement the interface method, but instead define new (unrelated) methods.
P.S. The correct spelling (in English) would be "applyBrakes".
回答2:
"java:com.company.Bicycle is not abstract and does not override abstract method applyBreakes(int) in com.company.Vehicle and same for the Bike class."
Means that the Bicycle class has a number of methods, but not enough methods to be a fully defined class.
In this case, the missing method is the one that Vehicle says must be implemented, the applyBreaks(int) method.
回答3:
The method to implement in the interface is applyBreakes() but you implement applyBreaks() in the concrete class.
It seems to be a spelling error.
Fortunately, you can protect you against this kind of compilation error early if you annotate all your overriding/implementing methods with @Override.
You did it with for example :
@Override
public void speedUp(int increment) {
speed = speed + increment;
}
but you didn't do it with :
public void applyBreaks(int decrement) {
speed = speed - decrement;
}
Annotate it with @Override and the compilation error message would be more explicit :
@Override
public void applyBreaks(int decrement) {
speed = speed - decrement;
}
来源:https://stackoverflow.com/questions/45174119/error-class-is-not-abstract-and-does-not-override-abstract-method-method