Interfaces
"a contract that spells out how software developed by different groups of programmers interacts."
A class that implements an interface must implement all the methods declared in the interface.
cf. Interface name: usually an adjective
Q. What if we want to make changes in an interface?
1. Create an interface that extends the original interface we made
2. Define new methods as default methods
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
Default Methods
Ex. Adding a new method, to the TimeClient interface
ZonedDateTime getZonedDateTime(String zoneString);
Original Interface
Not a desirable way
We have to modify the SimpleTimeClient.
Recommended Way - default method
The public access specifier indicates that the interface can be used by any class in any package.
Another class invoking the new method added in the original interface
Ex. TestSimpleTimeClient invokes the method getZonedDateTime from an instance of SimpleTimeClient.
Reminder!
If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
Extending Interfaces That Contain Default Methods
You can do the following,
1. Not mention the default method at all, which lets your extended interface inherit the default method.
2. Redeclare the default method, which makes it abstract.
3. Redefine the default method, which overrides it.
Static Methods in Interface
A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.