Implementing Interface: Implicit, Explicit, and Default
Learn about implicit and explicit interface implementations, how to define interfaces with default implementations, and their usage.
Implicit and explicit interface implementations
Interfaces can be implemented implicitly and explicitly. Implicit implementations are simpler and more common. Explicit implementations are only necessary if a type must have multiple methods with the same name and signature.
For example, IGamePlayer
and IKeyHolder
might have a method called Lose
with the same parameters because both a game and a key can be lost. In a type that must implement both interfaces, only one implementation of Lose
can be the implicit method. If both interfaces can share the same implementation, that works, but if not, then the other Lose
method will have to be implemented differently and called explicitly, as shown in the following code:
Get hands-on with 1400+ tech skills courses.