J
John Salerno
Along with events and delegates, polymorphism has been something I sort
of struggle with every now and then. First, let me quote the book I'm
reading:
"Polymorphism is most useful when you have two or more derived classes
that use the same base class. It allows you to write generic code that
targets the base class rather than having to write specific code for
each object type."
And here is the example in the book:
public virtual string GetDisplayText(string sep) // located in
Product base class
{
return code + sep + price.ToString("c") + sep + description; //
code, price, and description are private fields (for properties); sep is
a seperator character, like tab or newline
}
public override string GetDisplayText(string sep) // in Book subclass
{
return base.GetDisplayText() + sep + author;
}
public override string GetDisplayText(string sep) // in Software subclass
{
return base.GetDisplayText() + sep + version;
}
First off, a quick question that just popped into my head: When
GetDisplayText is called from the two overridden methods, why doesn't it
have an argument?
Ok, the real question: If you are going to override a virtual method
with another method that is more specific to a particular subclass, like
the two overridden methods above, then how does this accomplish what the
above quote says? I see that the virtual method is generic for any
Product object, but if you have to override methods for the subclasses
in order to use polymorphism at all, then how exactly does this prevent
"having to write specific code for each object type"?
Thanks,
John
of struggle with every now and then. First, let me quote the book I'm
reading:
"Polymorphism is most useful when you have two or more derived classes
that use the same base class. It allows you to write generic code that
targets the base class rather than having to write specific code for
each object type."
And here is the example in the book:
public virtual string GetDisplayText(string sep) // located in
Product base class
{
return code + sep + price.ToString("c") + sep + description; //
code, price, and description are private fields (for properties); sep is
a seperator character, like tab or newline
}
public override string GetDisplayText(string sep) // in Book subclass
{
return base.GetDisplayText() + sep + author;
}
public override string GetDisplayText(string sep) // in Software subclass
{
return base.GetDisplayText() + sep + version;
}
First off, a quick question that just popped into my head: When
GetDisplayText is called from the two overridden methods, why doesn't it
have an argument?
Ok, the real question: If you are going to override a virtual method
with another method that is more specific to a particular subclass, like
the two overridden methods above, then how does this accomplish what the
above quote says? I see that the virtual method is generic for any
Product object, but if you have to override methods for the subclasses
in order to use polymorphism at all, then how exactly does this prevent
"having to write specific code for each object type"?
Thanks,
John