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

How to do it...

  1. Create interfaces that will describe the API:
        public interface SpeedModel {          double getSpeedMph(double timeSec, int weightPounds,                             int horsePower);        }        public interface Vehicle {          void setSpeedModel(SpeedModel speedModel);          double getSpeedMph(double timeSec);        }        public interface Car extends Vehicle {          int getPassengersCount();        }        public interface Truck extends Vehicle {          int getPayloadPounds();        }
  1. Use factories, which are classes that generate objects that implement certain interfaces. A factory is an implementation of a pattern for creating objects without having to specify the exact class of the object that is created--specifying an interface only, rather than calling a constructor. It is especially helpful when an instance of ...

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