[Mono-list] Please help me with polimorfism, etc. (possible bug in the compiler?)

Roberto Jimeno jimeno@servidor.unam.mx
23 Sep 2004 00:27:23 -0500


--=-glqN4mwuXvIZU+crco79
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Please help me monoers,

some days ago I was overriding Object's Equals() when I came across what
I thought to be a bug. Now I think I'm simply being confused about
inheritance, polimorfism, substitutability principle and such. Please
compile and execute the small example (attached file). You will see the
string:

"Types of self and argument are derivedClass and derivedClass
respectively."

, while I was expecting to see

"Types of self and argument are derivedClass and baseClass
respectively."

Can you tell me which changes (a suggestions is commented out in the
code) should I apply to my code in order to obtain the "baseClass"
expected result?

TIA


--=-glqN4mwuXvIZU+crco79
Content-Disposition: attachment; filename=bug-substitutibilidad.cs
Content-Type: text/plain; name=bug-substitutibilidad.cs; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

using System;

class baseClass
{
  public Void show(Object o)
  {
    String s="Types of self and argument are {0} and {1} respectively.";
    Console.WriteLine(s, this.GetType(), o.GetType());
  }
}

class derivedClass : baseClass
{
  public new Void show(Object o)
  {
    base.show((baseClass)o);
    // if(o is baseClass) base.show(o as baseClass);
  }
}

class main
{
  static baseClass o = new derivedClass(); 
  
  public static void Main()
  {
    o.show(o);
  }
}

--=-glqN4mwuXvIZU+crco79--