C# poly and method hiding issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • harrylmh

    C# poly and method hiding issue

    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
  • Sami Vaaraniemi

    #2
    Re: C# poly and method hiding issue

    "harrylmh" <minghong@hotpo p.com> wrote in message
    news:d0pe70tvmb i8uldnogagoaevq nuqhcf1tp@4ax.c om...[color=blue]
    > 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[/color]


    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


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: C# poly and method hiding issue

      harrylmh <minghong@hotpo p.com> wrote:[color=blue]
      > 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?[/color]

      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.
      [color=blue]
      > Also, what's the difference between method hiding and overriding when
      > they're both still overriding the base method.[/color]

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

      See http://www.pobox.com/~skeet/csharp/faq/#override.new

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...