What is implicit interface in C#? How explicit interface is different than implicit interface?

(Last Updated On: April 8, 2017)

Answer includes implicit interface in C# programming with example and will also differentiate with explicit Interface implementation. In an interview, we need to explain both by program example and not with the definition only.

Answer: In C#, an Interface can be implemented implicitly and explicitly. Here is the explanation of both implicit and explicit interface by example.

When we implement an interface implicitly, we need to note two points

  • Class will implement interface method with “public” modifier.
  • Implanted interface method will be called on class object/interface reference

I have written one more interview question on explicit interface i.e. If 2 interfaces have method with same name does not it conflict? how would you call?

Implicit interface implementation example in C#

In below program example, class Maestro will implement the interface ICreditCard method using public modifier and implemented method will be call using class object or interface reference in main()

C# Code Example

 

Explicit interface implementation example in C#

For explicit interface implementation, we also need to note two points

  • Method implementation will contain interface name e.g.void ICreditCard.getCardInfo()in the class.
  • It should not contain public modifier.

In explicit implementation class, method can only be called by assigning class object to interface reference and not on the class object.

C# Program Example

 

Implicit Interface and Explicit Interface – Important points:
  • Explicit interface implemented method cannot be virtual or abstract while implicit interface in c# implementation in a class can be virtual and abstract.
  • Interface method implemented implicitly in class should be public while for explicit implanted method should be private, because, explicit method in a class is implemented using interface name e.g. void ICreditCard.getCardInfo(), hence, there is no sense using public.