- An interface is a reference type, similar to a class that can contain only constants, method signatures, default methods, static methods and nested types.
- Interfaces cannot be instantiated - they can only be implemented by classes and extended by other interfaces.
- Note that an interface can extend any number of interfaces. The interface declaration can include a comma-separated list of all the interfaces that it extends.
The interface body can contain abstract methods, default methods and static methods. All of these methods in an interface are implicitly
A class can implement more than one interface. The
implements keyword is followed by a comma-separated list of the interfaces implemented by the class.
When a new interface is defined, a new reference data type is defined. The interface names can be used anywhere a data type can be used. If a variable is assigned an interface type, any object assigned to it must be an instance of a class that implements the interface.
To avoid the scenario where implementing classes need to change because of the additions to the implemented interface:
- Create a new interface that extends to the changed interface. In this way, implementing classes only need to change what interface to implement, whether the child or the parent. Now users of the code can choose to continue to use the old interface or to upgrade to the new interface.
- Alternatively, new methods can be defined as default methods. Since implementations are provided for default methods, users who have classes that implement interfaces enhanced with default or static methods do not have to modify or recompile them to accomodate the additional methods.
Default methods enable one to add new functionality to the interfaces of the library to ensure binary compatibility with code written for older versions of that interfaces.
- A default method in an interface is defined with the
defaultkeyword at the beginning of the method signature. All method declarations in an interface, including default methods are implicityly public so the
publicmodifier can be omitted.
When an interface with a default method is extended,
- There is no need to mention the default method at all, which lets the extended interface inherit the default method
- The default method can be redeclared which makes it abstract
- The default method can be redefined by overriding it
A static method can also be defined in an interface. It 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. Now this helps in organizing helper methods.
Default methods enable the addition of new functionality to existing interfaces and ensure binary compatibilty with code written for older versions of those interfaces. In particular, default methods enable the addition of methods that accept lambda expressions as parameters to existing interfaces.
- An interface declaration can contain method signatures, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods
- A class that implements an interface must implement all the mothods declared in the interface.
- An interface name can be used anywhere a type can be used.
Cheers to continued learning 🍻!