Is base method able to use derived base data members?
- by iTayb
Lets assume we have the following code:
abstract class Base1 {
protected int num;
}
class Der1:Base1 {
protected Color color;
protected string name;
}
class Der2:Base1 {
protected DateTime dthen;
}
and so on. An array of base1 type exists and includes many objects created out of classes that are derived from base1.
Is it possible to define the toString() method in the base class only?
something like:
public override string toString()
{
if (this is Der1)
return "num = " + this.num + "color = " + this.color.toString() + " name = " this.name;
if (this is Der2)
return "num = " + this.num + "dthen = " + this.dthen.toString();
// and so on ...
}
Thank you very much :)
P.S. This is not an homework question. I've just wondered about.