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. Add the getWeightKg() implementation too. Since we do not have the getWeightPounds() method in the interface, the method signature should include an input parameter:
        public interface Truck extends Vehicle {          int getPayloadPounds();          default int getPayloadKg(){            return (int) Math.round(0.454 * getPayloadPounds());          }          static int convertKgToPounds(int kilograms){            return (int) Math.round(2.205 * kilograms);          }          default int getWeightKg(int pounds){            return (int) Math.round(0.454 * pounds);          }        }
  1. Remove the redundant code using the private interface method:
        public interface Truck extends Vehicle {          int getPayloadPounds();          default int getPayloadKg(int pounds){            return convertPoundsToKg(pounds);          } static int convertKgToPounds(int kilograms){ ...

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