错误:“<class>不是抽象的,不会覆盖抽象方法<method>”

时间:2017-07-18 18:06:29

标签: java interface

错误:

  

java:com.company.Bicycle不是抽象的,不会覆盖com.company.Vehicle中的抽象方法applyBreakes(int),也不会覆盖Bike类的相同内容。

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

}
}

3 个答案:

答案 0 :(得分:1)

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

接口方法名称为totalDays = abs((CreateDate - todaysDate).days),而实现称之为{ "errorMessage": "datetime.datetime(2017, 1, 11, 0, 0) is not JSON serializable", "errorType": "TypeError", "stackTrace": [ [ "/var/lang/lib/python3.6/json/__init__.py", 238, "dumps", "**kw).encode(obj)" ], [ "/var/lang/lib/python3.6/json/encoder.py", 199, "encode", "chunks = self.iterencode(o, _one_shot=True)" ], [ "/var/lang/lib/python3.6/json/encoder.py", 257, "iterencode", "return _iterencode(o, 0)" ], [ "/var/runtime/awslambda/bootstrap.py", 110, "decimal_serializer", "raise TypeError(repr(o) + \" is not JSON serializable\")" ] ] } 。 所以这些类不实现接口方法,而是定义新的(不相关的)方法。

P.S。正确的拼写(英文)将是&#34; applyBrakes&#34;。

答案 1 :(得分:1)

  

&#34; java:com.company.Bicycle不是抽象的,也不会覆盖   com.company.Vehicle中的抽象方法applyBreakes(int)和for   自行车课。&#34;

意味着Bicycle类有许多方法,但没有足够的方法成为完全定义的类。

在这种情况下,缺少的方法是applyBreakes所说的必须实现的方法,applyBreaks方法。

答案 2 :(得分:0)

在界面中实现的方法是applyBreakes(),但是在具体类中实现applyBreaks()
这似乎是一个拼写错误。

幸运的是,如果使用@Override注释所有覆盖/实现方法,则可以提前保护您免受此类编译错误的影响。

你是这样做的:

@Override
public void speedUp(int increment) {
    speed = speed + increment;
}

但你没有这样做:

public void applyBreaks(int decrement) {
    speed = speed - decrement;
}

使用@Override对其进行批注,编译错误消息将更明确:

@Override 
public void applyBreaks(int decrement) {
    speed = speed - decrement;
}