question about polymorphism

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
 
J

Jon Skeet [C# MVP]

John Salerno said:
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.
 
R

Richard Blewett [DevelopMentor]

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top