Default methods can be added to interfaces. They permit existing interfaces to be expanded to include new methods without breaking the older code. It is the default method of an interface that has an implementation. For example, consider a class that implements an interface, and then later a default method is added to the interface. The class has to implement all of the interfaces' abstract methods or the class will be abstract. However, since a default method is not abstract and has an implementation, it does not affect the class.
The addition of functions to Java suggests the need to incorporate their use with older classes and interfaces. After all, it is desirable to take advantage of existing code when possible to avoid rewriting it. Many of the default method additions to existing Java packages have been done with the intent of supporting functions.
Default methods consist of the keyword, default
, followed by the declaration of a method and its implementations...