Solved

When do you use polymorphism?

Posted on 2012-04-07
7
511 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now