473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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
Nov 16 '05 #1
6 3321
"harrylmh" <mi******@hotpop.com> wrote in message
news:d0********************************@4ax.com...
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
Nov 16 '05 #2
harrylmh <mi******@hotpop.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
On Sat, 10 Apr 2004 03:03:39 +0000, harrylmh wrote:
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#.
Nov 16 '05 #4
"harrylmh" <mi******@hotpop.com> wrote in message
news:d0********************************@4ax.com...
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?".
Nov 16 '05 #5
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

On Sat, 10 Apr 2004 01:10:34 -0700, "Sami Vaaraniemi"
<in**@capehill.net> wrote:
"harrylmh" <mi******@hotpop.com> wrote in message
news:d0********************************@4ax.com.. .
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


Nov 16 '05 #6
harrylmh <mi******@hotpop.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: harrylmh | last post by:
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,...
1
by: Thierry Lam | last post by:
I have a piece of python code which goes like the following: import poly list = result = poly.scan(list) I'm using Python 2.3.4 and I don't think that poly library is working properly,...
5
by: PIEBALD | last post by:
I was trying to break some polymorphism, expecting it not to work, but I'm a curious sort. I was seeing what happens when a derived class tries to hide an inherited method with a private new...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
4
by: Alan T | last post by:
I got a method in my ancestor form declared as Protected, this method has empty body. In my descendant form I declared as Protected also, then compile has no problem but the name of the method has...
9
by: Andrew Robinson | last post by:
I have class A which inherits from class B. B contains the following virtual method: public virtual List<TSelect() { return new List<T>(); } Is there any way to make private or otherwise...
11
by: Alex | last post by:
Hello all, I have a main form(say "form1") .i want to display another form(say "form2") on occuring of an event (say a button click) and want to hide it after some time so that it will again...
2
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.