Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How are interface members accessed in an interface?


Asked by Jensen Tran on Dec 05, 2021 FAQ



In an explicit interface implementation, the interface members can only be accessed through an interface instance (e.g. by casting the class to the interface type). The interface members are not visible as part of the class itself.
Similarly,
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
And, A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface. In addition, default interface members can only be accessed through an instance of the interface.
Indeed,
You can have explicit access modifiers for interface members. You can also have private interface members with default implementations. The default access level is public but you can also specify explicitly. You can also have protected, internal, and protected internal interface members.
Likewise,
Therefore, the members of an interface cannot be protected. If you try to declare the members of an interface protected, a compile time error is generated saying “modifier protected not allowed here”. In the following Java example, we are trying to declare the field and method of an interface protected.