Java Reference
In-Depth Information
length = 0;
width = 0;
radius = measurementValues[0];
}
if (radius == 0){
return length * width * avgDepth;
} else {
return (radius * radius) * 3.14 * avgDepth;
}
};
}
Any class implementing this interface can now use this default method implement-
ation or provide its own implementation, much like an abstract class.
How It Works
A standard Java interface consists of method declarations that do not contain a body.
Classes implement Java interfaces, and in doing so they provide implementations for
the method declarations of the interface. Java interfaces are an important part of object
orientation, as they abstract implementation details and provide a facade to extend
functionality. In Java 8, interfaces have been changed to allow default method imple-
mentations. A default method is a method declaration and implementation, and it is de-
noted with the default keyword preceding the declaration. Any class that extends an
interface containing a default method can choose to implement and override the default
implementation or simply use the default implementation.
You may wonder why this functionality was provided in Java 8. Why not simply
use abstract classes, rather than default methods? Default methods were added in order
to provide a way to change interfaces without breaking backward compatibility. Many
interfaces needed to be changed to accommodate Java 8 API updates, and in doing so,
these interfaces would possibly break functionality of code that was written for prior
releases of Java. By adding default methods to interfaces, engineers were able to add
new functionality to existing interfaces without breaking the code that relied on those
interfaces. To learn more about default methods, refer to Recipe 5-7.
Search WWH ::




Custom Search