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...

Java provides a capability to extend a class and allow the subclass to access all of the functionality of the base class. For example, you can decide that every object that could be asked about its speed belongs to a subclass that is derived from the Vehicle class. In such a case, Car may look like this:

public class Car extends Vehicle {  private int passengersCount;  public Car(int passengersCount, int weightPounds,              Engine engine){    super(weightPounds, engine);    this.passengersCount = passengersCount;  }  public int getPassengersCount() {    return this.passengersCount;  }}

Now we can change our test code by replacing the Vehicle class with the Car class:

public static void main(String... arg) {   double timeSec = 10.0;  int horsePower ...

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