Solved

When do you use polymorphism?

Posted on 2012-04-07
7
522 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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