Yes it will compile, but the derived class will hide (not override) the member of the base class (which the compiler will warn you of unless you explicitly use the new
keyword).
You can use this simple program to see what happens:
void Main()
{
IEntity e1 = new Base();
IEntity e2 = new Derived();
Base b1 = new Base();
Base b2 = new Derived();
Derived d = new Derived();
e1.Test();
e2.Test();
b1.Test();
b2.Test();
d.Test();
}
public class Base : IEntity
{
public void Test()
{
Console.WriteLine("Base");
}
}
public class Derived: Base, IEntity
{
public void Test()
{
Console.WriteLine("Derived");
}
}
// Define other methods and classes here
public interface IEntity
{
void Test();
}
The output you'll get (with explanation added) is:
Base (will call base member)
Derived (will call derived member)
Base (will call base member)
Base (will call base member since the variable is the base type)
Derived (will call derived member)
Here's where the difference in implementing the interface in the derived class comes. Try this:
((IEntity)d).Test();
This will output Derived
_if Derived
implements IEntity
. If Derived
does not implement IEntity
, then the compiler will bind to the base class method even though Derived
implements Test
also. This emphasizes that Derived
hides the interface method rather than re-implements it.