468,719 Members | 1,787 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,719 developers. It's quick & easy.

question about polymorphism

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
Nov 17 '05 #1
3 1428
John Salerno <jo******@NOSPAMgmail.com> wrote:
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?
It should have - unless you've got another method with no parameters in
the base class, the above code won't compile.
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"?


You don't have to write different code to *use* each different type.
Instead of writing:

string x;
if (foo is Software)
{
x = ((Software)foo).GetSoftwareDisplayText(...);
}
else if (foo is Book)
{
x = ((Book)foo).GetBookDisplayText(...);
}

you can just write:

string x = foo.GetDisplayText(...);

and the right version will be called.

Take another example - Streams. I usually don't care whether I'm
reading from a file, memory, or the network when I'm reading from a
Stream - I just want to read the data. Because they all inherit from
the same base class, I can treat them all the same way. Note that
interfaces provide this ability too - using a base class allows
inheritance of implementation as well as interface.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Jon Skeet [C# MVP] wrote:
You don't have to write different code to *use* each different type.
Instead of writing:


Ah, that makes it a lot clearer! Thanks!
Nov 17 '05 #3
1) The example is badly written and should pass the param to the base class version

2) The power of polymorphism is that you can write code that is based on using a base class reference and calls methods on it. However the actual code that runs is based on the type of the object that is hooked up to the reference. In your example

void PrintProduct(Product p)
{
Console.WriteLine(p.GetDisplayText(","));
}

This will print out different things if the actual object passed to the method is a Book or a Software object. So this is what is meant by writing generic code based on the base class rather than having to write the GetDisplayText method with special coding for Books and Software

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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

[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Liza | last post: by
7 posts views Thread by DJ.precario | last post: by
8 posts views Thread by Locia | last post: by
12 posts views Thread by Meya-awe | last post: by
2 posts views Thread by sarathy | last post: by
9 posts views Thread by davetelling | last post: by
90 posts views Thread by John Salerno | last post: by
7 posts views Thread by Markus Svilans | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.