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

Well, with the getWeightKg(int pounds) method accepting the input parameter, the method name can be misleading because (by contrast, with getPayloadKg()) the method does not control the source of the data. The input value can represent anything. After realizing it, we decided that the interface would be better without it, but only after making the convertPoundsToKg() method public. Since it does not require access to the object elements, it can be static too:

public interface Truck extends Vehicle {  int getPayloadPounds();  default int getPayloadKg(int pounds){    return convertPoundsToKg(pounds);  }  static int convertKgToPounds(int kilograms){    return (int) Math.round(2.205 * kilograms);  } static int convertPoundsToKg(int pounds){ ...

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