C# poly and method hiding issue

  • Thread starter Thread starter harrylmh
  • Start date Start date
H

harrylmh

Hi,
I'm learning C# and I just don't quite understand the need for
polymorphism. why do we need to use it? how does a base class variable
holding a derived class instance do any good?

Also, what's the difference between method hiding and overriding when
they're both still overriding the base method.

Thanks
 
harrylmh said:
I'm learning C# and I just don't quite understand the need for
polymorphism. why do we need to use it? how does a base class variable
holding a derived class instance do any good?

I can write a method which (say) reads the contents of one stream,
encrypts it, and writes it out to another stream. I can do that without
knowing what kind of stream I'm writing to (or reading from) at all -
it could be over the network, or a file, or memory, or something
completely different.
Also, what's the difference between method hiding and overriding when
they're both still overriding the base method.

They're not both overriding the base method - one is only hiding it.

See http://www.pobox.com/~skeet/csharp/faq/#override.new
 
harrylmh said:
Hi,
I'm learning C# and I just don't quite understand the need for
polymorphism. why do we need to use it? how does a base class variable
holding a derived class instance do any good?

Also, what's the difference between method hiding and overriding when
they're both still overriding the base method.

Thanks


Polymorphism allows you to treat instances of different classes in a uniform
manner. Suppose e.g., that you have a container of objects that are all
subclasses of the type Drawable, say Rectangles and Circles. Suppose also
that class Drawable contains a virtual method Draw that the subclasses
override. You can then draw all the objects on the screen with code like
this:

foreach (Drawable d in drawables)
d.Draw(graphics);

The real power of this approach comes from its extensibility. You can add a
completely new Drawable subtypes and your loop above would work without you
having to change it. Compare that to the C-style code where you would have a
switch on object type and call to a specific drawing function based on the
type. You would have to update this switch statement every time a new class
was introduced - this would be a maintenance problem.

Method hiding, on the other hand, is something that actually breaks
polymorphism. If one of you Drawable subclasses would declare a new Draw
method like so:

public new void Draw(Graphics g)
{
//...
}

Then the loop above would end up calling Drawable.Draw for instances of this
type. In general this is something that you don't want to happen.

Regards,
Sami
 
Hi,
I'm learning C# and I just don't quite understand the need for
polymorphism. why do we need to use it? how does a base class variable
holding a derived class instance do any good?

Also, what's the difference between method hiding and overriding when
they're both still overriding the base method.

Thanks

You haven't grokked it yet... object oriented thinking that is. Put aside learning
C# for now and do yourself a favour... read a decent introductory text on
object oriented design, then study C#.
 
harrylmh said:
I'm learning C# and I just don't quite understand the need for
polymorphism. why do we need to use it?

This question was asked and answered on this group 2 days ago. Please search
for the thread "Advantages of Ploymorphism?".
 
Hi,
if using method hiding causes the base class method to be called when
using polymorphism, what's the use ot it? method overriding seems to
be more useful.

Thank you
 
harrylmh said:
if using method hiding causes the base class method to be called when
using polymorphism, what's the use ot it? method overriding seems to
be more useful.

Indeed it is. Method hiding is only useful if you really *need* a new
method with the same name as a base one. Usually this would be either
to provide a more strongly type version of a base method, just for ease
of use within the class, or if the base class gained an extra method
with the same name as an existing method in the derived class in a
later version (of the base class).
 
Back
Top