Implementation

An interface can be implemented by a class, which means that the class has a body for each of the abstract methods listed in the interface. Here is an example:

interface Car {  double getWeightInPounds();  double getMaxSpeedInMilesPerHour();}public class CarImpl implements Car{  public double getWeightInPounds(){    return 2000d;  }  public double getMaxSpeedInMilesPerHour(){    return 100d;  }}

We named the class CarImpl to indicate that it is an implementation of the interface Car. But we could name it any other way we like. 

Both interface and its class implementation can have other methods too without causing a compiler error. The only requirement for the extra method in the interface is that it has to be default and have a body. Adding ...

Get Introduction to Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.