O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

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

How it works...

Let's write a test program:

public static void main(String... arg) {  double timeSec = 10.0;  int engineHorsePower = 246;  int vehicleWeightPounds = 4000;  Vehicle vehicle = new Car(4, vehicleWeightPounds, engineHorsePower);  System.out.println("Passengers count=" +                                  ((Car)vehicle).getPassengersCount());  System.out.println("Car speed (" + timeSec + " sec) = " +                                vehicle.getSpeedMph(timeSec) + " mph");  vehicle = new Truck(3300, vehicleWeightPounds, engineHorsePower);  System.out.println("Payload=" +                            ((Truck)vehicle).getPayload() + " pounds");  System.out.println("Truck speed (" + timeSec + " sec) = " +                                vehicle.getSpeedMph(timeSec) + " mph");}

Notice that the vehicle reference of the Vehicle type points to the object of the Car

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