0

I have 2 classes named Family and Member. Member derives from Family. I also have an Interface named IDatabase. Both the Family and Member classes implement IDatabase. Will this produce a compiler error?

To clarify, let's say IDatabase has 3 methods: Add, Update, and Delete. If both classes implement this interface, does the compiler throw errors because both classes contain methods with the same name?

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
David
  • 57
  • 1
  • 6
  • 6
    Why don't you just compile it and see ? – Habib Sep 11 '14 at 17:38
  • The compiler will work just fine with it, but seriously, you should just compile and ask why you get an error you don't expect (if you got an error, which you shouldn't in this case). – BradleyDotNET Sep 11 '14 at 17:39
  • Are you talking about something like [this](http://stackoverflow.com/questions/25008021/why-does-this-c-sharp-code-return-what-it-does)? This is a feature of the language called interface re-implementation. – Mike Zboray Sep 11 '14 at 17:46
  • it shouldn't throw an error but it should warn you about using "new" keyword if you intended to hide the member. – Bozman Sep 11 '14 at 17:54
  • And even if you don't have direct access to Visual Studio, you can test something like this with a [Dot Net Fiddle Example](https://dotnetfiddle.net/Mb7sIm). – Erik Philips Sep 11 '14 at 17:56

2 Answers2

2

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Most people think adding the IEntity to the derived class is just redundant, but in fact it can result in a change in behavior. In this case, removing it causes the second line in the output to be "Base". – Mike Zboray Sep 11 '14 at 17:52
  • @mikez Correct, I added that distinction to my explanation. – D Stanley Sep 11 '14 at 17:53
1

Put virtual on the base class' implementation and override on the specific class' implementation

e.g.

public interface
{
    void MyMethod();
}

public class MyBaseClass : IMyInterface
{
    public virtual void MyMethod()
    {
    }
}

public class MyInhertingClass : MyBaseClass
{
    public override void MyMethod()
    {
    }
}
Danny Varod
  • 17,324
  • 5
  • 69
  • 111