c# - Implementing an interface in a base and derived class -


in c#, can give example of why implement interface on base class , re-implement interface on derived class, rather making base class methods virtual.

for example:

interface imakesnoise {   void speak(); }  class cat : imakesnoise {   public void speak()   {     console.writeline("meow");   } }  class lion : cat, imakesnoise {   public new void speak()   {     console.writeline("roar");   } } 

to test behavior:

cat cat = new cat(); cat lion = new lion();  // non virtual calls, acts expected     cat.speak(); lion.speak();  // grabbing interface out 'virtual' in grabs derived interface implementation (cat imakesnoise).speak(); (lion imakesnoise).speak(); 

this print out:

meow meow meow roar 

update: more clarification why, reason implementing compiler , want know reason c# chose implementation of interfaces.

i have looked @ question.

interface inheritance in comvisible classes in c#

and this

c# exposing com - interface inheritance

as understand, if have 2 objects , want them visible through com, both should explicitly inherit required interface.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -