Solved

When do you use polymorphism?

Posted on 2012-04-07
7
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best book for Internet security 4 55
C# LINQ query question 11 33
Difference between Highcharts and Mapbox 10 45
Manage big list of parameter list 8 26
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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