C#.NET — Consider explicit interface implementation with a partially implemented interface. interface IMyInterface { void fun1(); void fun2(); } class MyClass : IMyInterface { private int i; void IMyInterface.fun1() { /* code */ } // fun2 not implemented here } What is correct?

Difficulty: Easy

Correct Answer: The compiler will report an error since the interface IMyInterface is only partially implemented.

Explanation:


Introduction / Context:
In C#, a non-abstract class that declares it implements an interface must provide implementations for all interface members. Explicit interface implementation is permitted, but completeness is still required.



Given Data / Assumptions:

  • IMyInterface declares fun1 and fun2.
  • MyClass uses explicit implementation for fun1 only.
  • MyClass is not declared abstract.


Concept / Approach:
When a class lists an interface after the colon, the compiler enforces that every declared member of the interface is implemented (either explicitly or implicitly). If any member is missing and the class is not abstract, compilation fails. Declaring the class abstract would defer the unimplemented members to a subclass.



Step-by-Step Solution:

Identify interface members: fun1, fun2.Check implementations in MyClass: only fun1 is present (explicitly).MyClass is not abstract → must implement fun2 as well → violation.Result: compiler error stating MyClass does not implement interface member fun2.


Verification / Alternative check:
Add either an implicit public void fun2() { ... } or an explicit void IMyInterface.fun2() { ... }. Compilation will then succeed.



Why Other Options Are Wrong:

  • A: The class is not declared abstract.
  • B: Classes can contain instance data alongside interface implementations.
  • C: False — fun2 is missing.
  • D: Interfaces do not inherit from Object; classes do.


Common Pitfalls:
Forgetting that explicit implementation still requires completeness; it only affects accessibility and call syntax.



Final Answer:
The compiler will report an error since the interface IMyInterface is only partially implemented.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion