is not abstract and does not override abstract method "-6ren"> is not abstract and does not override abstract method "-错误: java:com.company.Bicycle is not abstract and does not override abstract method applyBreakes(int)-6ren">
gpt4 book ai didi

java - 错误: " is not abstract and does not override abstract method "

转载 作者:行者123 更新时间:2023-11-30 02:27:52 33 4
gpt4 key购买 nike

错误:

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

}
}

最佳答案

因为您在接口(interface)(和/或实现)中拼错了方法名称。

接口(interface)方法名称为applyBreaks,而实现则将其称为applyBreaks。因此,这些类不会实现接口(interface)方法,而是定义新的(不相关的)方法。

<小时/>

附注正确的拼写(英语)是“applyBrakes”。

关于java - 错误: "<Class> is not abstract and does not override abstract method <method>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174119/

33 4 0