Polymorphism and inheritance
-
Let's see if I can explain my small doubt. I can go around this, but as I was coding something, I got curious if something wasn't possible to do in c#. In C++, you could have something like this:
class A { public virtual void method() { // something } } class AA : public A { public void method() { // something else } } class AB : public B { public void method() { // even something else } } A *obj; if ( something ) { obj = (A)( new AA() ); } else { obj = (A)( new AB() ); } obj->method();
What happens here is that when you invoke the method function, you'll be executing the implementation from AA or from AB and not from A. Though, in C#, if I try to do something like this, you'll be executing the implementation from A. That fact to me, is a shame... I liked this behavior. And as pointers in C# only exist for values, I suppose this is not possible with C#. Best regardsGonçalo A.
-
Let's see if I can explain my small doubt. I can go around this, but as I was coding something, I got curious if something wasn't possible to do in c#. In C++, you could have something like this:
class A { public virtual void method() { // something } } class AA : public A { public void method() { // something else } } class AB : public B { public void method() { // even something else } } A *obj; if ( something ) { obj = (A)( new AA() ); } else { obj = (A)( new AB() ); } obj->method();
What happens here is that when you invoke the method function, you'll be executing the implementation from AA or from AB and not from A. Though, in C#, if I try to do something like this, you'll be executing the implementation from A. That fact to me, is a shame... I liked this behavior. And as pointers in C# only exist for values, I suppose this is not possible with C#. Best regardsGonçalo A.
Why dont you try it and see what happens?
using System;
using System.Collections.Generic;
using System.Text;namespace VirtualMethods
{
class Program
{
static void Main(string[] args)
{
A a = new A();
A aa = new AA();
A ab = new AB();
A ac = new AC();Console.WriteLine(a.GetValue()); Console.WriteLine(aa.GetValue()); Console.WriteLine(ab.GetValue()); Console.WriteLine(ac.GetValue()); Console.ReadLine(); } }
}
class A
{
public virtual string GetValue()
{
return "A";
}
}class AA : A
{
public override string GetValue()
{
return "AA";
}
}class AB : A
{
public override string GetValue()
{
return "AB";
}
}class AC : A
{
public override string GetValue()
{
return base.GetValue();
}
} -
Why dont you try it and see what happens?
using System;
using System.Collections.Generic;
using System.Text;namespace VirtualMethods
{
class Program
{
static void Main(string[] args)
{
A a = new A();
A aa = new AA();
A ab = new AB();
A ac = new AC();Console.WriteLine(a.GetValue()); Console.WriteLine(aa.GetValue()); Console.WriteLine(ab.GetValue()); Console.WriteLine(ac.GetValue()); Console.ReadLine(); } }
}
class A
{
public virtual string GetValue()
{
return "A";
}
}class AA : A
{
public override string GetValue()
{
return "AA";
}
}class AB : A
{
public override string GetValue()
{
return "AB";
}
}class AC : A
{
public override string GetValue()
{
return base.GetValue();
}
}override.... right. I feel like a newbie, but I guess these things happen when moving on to a new language :p Thank you for the quick reply, you helped a lot. :)
Gonçalo A.
-
override.... right. I feel like a newbie, but I guess these things happen when moving on to a new language :p Thank you for the quick reply, you helped a lot. :)
Gonçalo A.
no problem :) You can only override a virtual or abstract method, but you can "new" a method over any old one. i.e. class AA : A { public new string GetValue() { return "NEW"; } } But this will behave how you originally described ... if cast as 'A' it'll call the original method on the A class. It'll only call the 'new' method if cast as an AA.
-
no problem :) You can only override a virtual or abstract method, but you can "new" a method over any old one. i.e. class AA : A { public new string GetValue() { return "NEW"; } } But this will behave how you originally described ... if cast as 'A' it'll call the original method on the A class. It'll only call the 'new' method if cast as an AA.
Yes, that was how I was doing it. The original was a virtual method, but I expected him to override automatically. Though, it does make more sense this way, and offers more possibilities. Thank you, once again, for the explanation.
Gonçalo A.
-
Yes, that was how I was doing it. The original was a virtual method, but I expected him to override automatically. Though, it does make more sense this way, and offers more possibilities. Thank you, once again, for the explanation.
Gonçalo A.
Hi, you should pay attention to the messages the compiler emits. If you try and redefine a virtual method without either "new" or "override", a warning is issued. Don't lower the warning level (a project setting, 4 is a good value) to get rid of (some of) the messages, study the messages and adapt your code instead. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Happy 2008!
-
Hi, you should pay attention to the messages the compiler emits. If you try and redefine a virtual method without either "new" or "override", a warning is issued. Don't lower the warning level (a project setting, 4 is a good value) to get rid of (some of) the messages, study the messages and adapt your code instead. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Happy 2008!
Yes, I noticed that already. Thanks.
Gonçalo A.