Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

When do you use polymorphism?

Posted on 2012-04-07
7
Medium Priority
?
562 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 800 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 1200 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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