Solved

When do you use polymorphism?

Posted on 2012-04-07
7
518 Views
Last Modified: 2012-04-14
In C# or VB.Net when do you use polymorphism? Do you use it a lot in your projects or rarely?
I am assuming its very rare somebody would utilize polymorphism in their projects?

Can you give a more real life scenario when somebody should use polymorphism, other than draw() or Add()?

Thanks.
0
Comment
Question by:Ricky66
7 Comments
 
LVL 3

Assisted Solution

by:fjocke
fjocke earned 200 total points
ID: 37820249
This is pretty common place is programming and more so in OOP.
Imagine you are doing a rpg game, and your parent class Character
has diffrent sets of functions that should apply to all different Characters in the game.

For instance having the functions walk or run as pure calls, should probably end up controlling diffrent speeds for different characters, especially if you want to implement stuff like classes that gets different bonuses when they level up.

This is as you already noticed a praxis that is used in game engines, for instance draw and update.

Hope this makes you understand a little bit more :)
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 300 total points
ID: 37820632
It is used all over the place.

Take for example a method that sets the color of a Control to Red if it contains a negative value:

Without polymorphism, you would need to create many methods, one for each control that you want to have that feature

Public Sub RedNeg (ctl As TextBox)
Public Sub RedNeg (ctl As Label)
Public Sub RedNeg (ctl As MaskedTextBox)
... add as many as you can think of.

But knowing that all of these inherits from Control and thus implement the ForeColor property, you can use polymorphism to create a single method that handles them all:

Public Sub RedNeg (ctl As Control)
   If Val(ctl.Text)<0 Then
      ctl.ForeColor=Color.Red
   Else
      ctl.ForeColor=SystemColors.WindowText
   End If
End Sub

You can pass any type of Control to such a method, and it will work. This beats having to repeat the same code for each control.

Another example is grid controls and DataTables. Since the cells accept an Object as a value, you can feed them with any type data that can be returned by the database. Without polymorphism, you would need to have a grid for strings and another for integers, or you would have to implicitely create the columns by setting their types. Because of polymorphism, grids and datatables can be filled with very little code.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37820979
i would not like to quote an example, would rather like you to see these links below and understand, they are simple enough.
In .net it can be achieved using abstract/virtual methods or using new keyword, it basically means adding various behaviors to single type:-

http://msdn.microsoft.com/en-us/library/ms173152%28v=vs.80%29.aspx
http://www.codeproject.com/Articles/1445/Introduction-to-inheritance-polymorphism-in-C
http://msdn.microsoft.com/en-us/library/ms173152.aspx
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Ricky66
ID: 37822422
Thanks all for your replies.
JamesBurger - I may be wrong but your example doesn't seem to use polymorphism?
0
 
LVL 40
ID: 37823417
Microsoft's definition, one of the references cited by BuggyCoder: Through inheritance, a class can be used as more than one type... This is called polymorphism

TextBox, Label and MaskedTextBox all inherits from Control.

Through that inheritance, Control (a class) is used as more than one type (TextBox, Label, MaskedTextBox).

If you do not see my example as polymorphism, then you still have to understand the concept.
0
 

Author Comment

by:Ricky66
ID: 37845660
Thanks James. so from what I gather we need to use polymorphism anytime there are going to be child classes and each of them share the same attribute but needs to be implemented differently.
0
 
LVL 40
ID: 37846107
Close, but not quite it.

And the right term is derived classes, not child. TextBox and Label are derived from Control.

You do not "need" to use polymorphism. You use it mainly to make your life simpler when multiple classes share a common ancestor or interface and you want to use the properties and methods that they share through that mechanism. Instead of having to code multiple methods that do basically the same thing, you can have only one method that works will all the derived classes.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question