0
public class Vehicle {

  private double speed;
  private int wheels;

  public Vehicle() {

  }

  public Vehicle(double speed, int wheels) {
    this.speed = speed;
    this.wheels = wheels;
  }

}


public class Motorcycle extends Vehicle {

  private double engineSize;

  public Motorcycle(double speed, double engine) {
    super(speed, 2);
    this.engineSize = engine;
  }

}


public class Moped extends Motorcycle {


}

Since Mope extends Motorcycle why is it telling me Moped needs to make a constructor? I want Moped to work without having any constructor.

teemo timmy
  • 37
  • 2
  • 4

2 Answers2

4

Because Motorcycle doesn't have a default constructor, you can't use it in Moped - but you could add an empty constructor for Moped - like,

public class Moped extends Motorcycle {
    public Moped() {
        super(70, 50); //<-- or whatever values you want 
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

If a class does not have a no-args constructor, then every subclass has to call one of it's explicitly defined constructors via super(). This is an example of Java being a verbose language; it will not assume that it can inherit a constructor.

Jacob Ward
  • 21
  • 2