O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

Let's try to model a crew cab--a truck with multiple passenger seats that combines the properties of a car and a truck. Java does not allow multiple inheritances. This is another case where interfaces come to the rescue.

The CrewCab class may look like this:

class CrewCab extends VehicleImpl implements Car, Truck {  private int payloadPounds;  private int passengersCount;  private CrewCabImpl(int passengersCount, int payloadPounds,                      int weightPounds, int horsePower) {    super(weightPounds + payloadPounds          + passengersCount * 250, horsePower);    this.payloadPounds = payloadPounds;    this. passengersCount = passengersCount;  }  public int getPayloadPounds(){ return payloadPounds; }  public int getPassengersCount() {  return this.passengersCount; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required