Odd behavior of inheritance and method overriding?

  • Thread starter Thread starter Mark Doliner
  • Start date Start date
M

Mark Doliner

I'm seeing a weird problem with inheritance/overriding a
method/implicit casting, and someone suggested that I ask about it
here.

I have a class that is inheriting from another class. The base class
has a Write(byte x) method and a Write(ushort x) method. The child
class only overrides Write(ushort x). If I call
ChildClass.Write(byte), the child's ushort method gets called instead
of the base class's Write(byte x) method. Is that normal?

I'm using mono/mcs, but someone tested it in Windows for me and got
the same results. Here's a sample program...



using System;

public class Parent {
public static void DoStuff(byte x) {
Console.WriteLine("In parent method: DoStuff(byte x)");
}

public static void DoStuff(ushort x) {
Console.WriteLine("In parent method: DoStuff(ushort x)");
}
}


public class Child:Parent {
publicnew static void DoStuff(ushort x) {
Console.WriteLine("In child method: DoStuff(ushort x)");
}
}


public class TestProg {
public static void Main() {
Console.WriteLine("In Main()");
byte val = 4;
Child.DoStuff(val);
}
}




My output from that is:
In Main()
In child method: DoStuff(ushort x)

I would have expected it to be:
In Main()
In parent method: DoStuff(byte x)

Anyone know why that happens?
 
The reason this is happening is because you have 2 DoStuff methods with a
single parameter. The difference between the 2 methods is the parameter
TYPE being passed in and this gives it the unique signature. If you look at
the code
public class TestProg {
public static void Main() {
Console.WriteLine("In Main()");
byte val = 4;
Child.DoStuff(val);
}
}

you see you declare the 'val' variable as type byte. When you call the
DoStuff method, it uses the type of the parameter to find which method to
call, so it will call the DoStuff method that has the parameter signature of
byte. If you declare the 'val' variable as 'ushort', it would call the
DoStuff method of the child class...

public class TestProg {
public static void Main() {
byte val = 4;
Child.DoStuff( (ushort) val );
}
}

OR

public class TestProg {
public static void Main() {
ushort val = 4;
Child.DoStuff( val );
}
}


Hope this helps!

--
MARK TRUDGEON
(e-mail address removed)

This posting is provided "as is", with no warranties, and confers no rights.
 
fzjr4n said:
The reason this is happening is because you have 2 DoStuff methods with a
single parameter. The difference between the 2 methods is the parameter
TYPE being passed in and this gives it the unique signature. If you look at
the code


you see you declare the 'val' variable as type byte. When you call the
DoStuff method, it uses the type of the parameter to find which method to
call, so it will call the DoStuff method that has the parameter signature of
byte. If you declare the 'val' variable as 'ushort', it would call the
DoStuff method of the child class...

It *is* calling the DoStuff of the child class - and that's what's
confusing.

Basically, the fact that you've got a child class with an applicable
method seems to take precedence over everything else. It's a very
strange bit of the C# spec as far as I can see, and my guess is that
it's not working in the way that it's meant to be. Your case isn't *so*
bad, but there are stranger ones - if you have a virtual method DoStuff
(byte) in the base class, and then DoShort (ushort) and DoShort(byte)
in the derived class where the latter overrides the method in the base
class, it *still* would end up calling DoShort(ushort). Very odd.
 
I think this is because your methods are all static and because byte can be
converted to ushort. If you change the two "ushort" methods to "string", see
if you then get the parent's "byte" method. Then try the same experiment
with equivalent, non-static methods.

John Saunders
(e-mail address removed)
 
Back
Top