Hi @Shervan360 , Welcome to Microsoft Q&A,
When a base class implements an interface, it may provide a specific implementation for one or more members of the interface. If a derived class reimplements the same interface, it effectively overrides the behavior of the base class's implementation of the interface. For example, a base class may rely internally on its own implementation of interface members. If a derived class reimplements an interface, it may change the behavior of the interface methods in a way that breaks assumptions made by the base class.
using System;
namespace xxx
{
interface IExample
{
void DoWork();
}
class BaseClass : IExample
{
public void DoWork()
{
Console.WriteLine("BaseClass: Doing work...");
}
public void CallWork()
{
DoWork();
}
}
class DerivedClass : BaseClass, IExample
{
// Reimplementing the interface
void IExample.DoWork()
{
Console.WriteLine("DerivedClass: Doing different work...");
}
}
class Program
{
static void Main()
{
IExample example1 = new BaseClass();
example1.DoWork(); // Output: BaseClass: Doing work...
IExample example2 = new DerivedClass();
example2.DoWork(); // Output: DerivedClass: Doing different work...
DerivedClass derived = new DerivedClass();
derived.CallWork(); // Output: BaseClass: Doing work...
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.