   [C#] Interfaces
  If I have a Base class that implements an interface IDo, having another class derive from Base, where Derived reimplements IDo.Do(), it still causes the base version of IDo.Do() to be called.interface IDo    {   void Do();    }    class Base : IDo    {   public void Do()   {   }    }    class Derived : Base    {   public void Do()   {   }    }    class Program    {   static void Main(string[] args)   {  Base b = new Base();  b.Do();  // Base::Do  IDo i1 = b;  i1.Do(); // Base::Do  Derived d = new Derived();  d.Do();  // Derived::Do  IDo i2 = d;  i2.Do(); // Base::Do < need this to call Base::Do >   }    }Which of the following is the correct way to solve this?// Polymorphisminterface IDo    {   void Do();    }    class Base : IDo    {   virtual public void Do()   {   }    }    class Derived : Base    {   override public void Do()   {   }    }    class Program    {   static void Main(string[] args)   {  Base b = new Base();  b.Do();  // Base::Do  IDo i1 = b;  i1.Do(); // Base::Do  Derived d = new Derived();  d.Do();  // Derived::Do  IDo i2 = d;  i2.Do(); // Derived::Do    }    }// re-including the interfaceinterface IDo    {   void Do();    }    class Base : IDo    {   public void Do()   {   }    }    class Derived : Base, IDo    {   public void Do()   {   }    }    class Program    {   static void Main(string[] args)   {  Base b = new Base();  b.Do();  // Base::Do  IDo i1 = b;  i1.Do(); // Base::Do  Derived d = new Derived();  d.Do();  // Derived::Do  IDo i2 = d;  i2.Do(); // Derived::Do    }    }Or is there another way?
Quote:  Derived d = new Derived();  d.Do();  // Derived::Do  IDo i2 = d;  i2.Do(); // Base::Do < need this to call Base::Do >There is no way to change which function is called based on the type of the variable you access it through. If the function is virtual, the most-derived function is resolved at runtime. However, something is amiss in this explanation, as the above will call Derived::Do for both.It might be doable with two functions, only one of which is overridden in the Base class. It depends though.Can you clarify what you want to do? You might want to describe what you are actually trying to achieve. Your inheritance hierarchy seems a little dysfunctional if I understand what you desire.   ;Quote:Original post by rip-offsomething is amiss in this explanation, as the above will call Derived::Do for both.No, the second one calls Base::Do. C# interfaces are different from Java interfaces. When a class implements an interface, all the interface methods are implicitly sealed so derived classes can't override them. If you want to allow derived classes to override them, you have to explicitly mark them as virtual in the base class.Read this for more info.   ;  My apologies, sorry.   ;Quote:No, the second one calls Base::Do. C# interfaces are different from Java interfaces. When a class implements an interface, all the interface methods are implicitly sealed so derived classes can't override them. If you want to allow derived classes to override them, you have to explicitly mark them as virtual in the base class.So like I did in my second example?   ;  No, in the second example you are not actually overriding the method from Base. Add this to the end of Main:Base b = d;b.Do();This will call Base.Do(). Derived.Do() will only be called if you are calling it using a reference to IDo (like in your example).Read the article I linked earlier for more info.   