School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,443 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,538 people online right now. Registration is fast and FREE... Join Now!




Polymorphism, what is your understanding?

 

Polymorphism, what is your understanding?

SixOfEleven

2 May, 2009 - 04:35 PM
Post #1

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
Polymorphism is an important concept in object-oriented programming. It seems to mean different things to different people. Try Googling it and you will see what I mean. smile.gif I think that these are two of the best definitions that I have found.

QUOTE
In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts.


I found that one in an article about polymorphism in C++.

This is the second:

QUOTE
In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B.


That one is from wikipedia.

This all stemmed from a topic in the C# http://www.dreamincode.net/forums/showtopic102750.htm

User is offlineProfile CardPM
+Quote Post


sl4ck3r

RE: Polymorphism, What Is Your Understanding?

2 May, 2009 - 05:32 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Sep, 2007
Posts: 261



Thanked: 6 times
Dream Kudos: 25
My Contributions
i dont see a problem ? they both describe it.
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Polymorphism, What Is Your Understanding?

2 May, 2009 - 06:29 PM
Post #3

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(sl4ck3r @ 2 May, 2009 - 05:32 PM) *

i dont see a problem ? they both describe it.


I was not saying there was a problem with them.

It just seemed to me that the first seems to lead toward the idea that polymorphism is all about overloading methods and operators inside a class because of the rest of the article.

The second seems to lead toward the idea that polymorphism is more about inheritance and virtual methods that will be changed when the object is inherited.

It is just can be a hard concept for somebody new to OOP might find difficult to understand and I was more trying to start a discussion on what polymorphism meant to others on this site so it would be easy for those new to OPP to understand.

I think I've found a bit better article that explains what polymorphism is to a programmer. (Though it was specifically from the C# programming guide on MSDN.)

QUOTE
Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism.


In an RPG polymorph is used to describe changing form from say a person into a dragon. smile.gif
User is offlineProfile CardPM
+Quote Post

KYA

RE: Polymorphism, What Is Your Understanding?

2 May, 2009 - 06:32 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,192



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
Damn it. I totally didn't see this thread before I made mine sad.gif

I forget to come to this forum on occasion. There's a similar thread in the C++ discussion area.

This post has been edited by KYA: 2 May, 2009 - 06:33 PM
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Polymorphism, What Is Your Understanding?

2 May, 2009 - 06:41 PM
Post #5

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(KYA @ 2 May, 2009 - 06:32 PM) *

Damn it. I totally didn't see this thread before I made mine sad.gif

I forget to come to this forum on occasion. There's a similar thread in the C++ discussion area.


I think it deserves a close look in both, it can be a tricky concept for people new to programming to understand and I don't think very many read this forum. smile.gif
User is offlineProfile CardPM
+Quote Post

programmer_temae

RE: Polymorphism, What Is Your Understanding?

3 May, 2009 - 08:14 PM
Post #6

New D.I.C Head
*

Joined: 1 May, 2009
Posts: 26



Thanked: 1 times
My Contributions
I will talk in terms of C++. Polymorphism is the hability to call to the functions of a derived object using a pointer of the base class type.
User is offlineProfile CardPM
+Quote Post

induster

RE: Polymorphism, What Is Your Understanding?

9 May, 2009 - 02:50 PM
Post #7

New D.I.C Head
*

Joined: 1 Mar, 2009
Posts: 18



Thanked: 2 times
My Contributions
A good example is the ability to pass in an inherited instance of an object to a method that accepts a parameter of the base classes type.

public void GetX(baseType bt){};

You could use this method as:

GetX(inheritedObjectInstance);

And this is valid because of dynamic binding.

Please correct me if I'm wrong, it's been a long time since working with this.
User is offlineProfile CardPM
+Quote Post

game_programmer

RE: Polymorphism, What Is Your Understanding?

31 Jul, 2009 - 09:17 AM
Post #8

New D.I.C Head
*

Joined: 31 Jul, 2009
Posts: 1

if there are 3 classes --BASE,derived1,derived2 and base contains a pure virtual function

then
Polymorphism simply means --

we can use the same function in different ways by just exchanging the addresses in the base class pointer..(exchanging with derived1 and derived2) wink2.gif
User is offlineProfile CardPM
+Quote Post

LoveIsNull

RE: Polymorphism, What Is Your Understanding?

1 Aug, 2009 - 11:32 AM
Post #9

Disbanding my Ignorance
****

Joined: 10 Mar, 2009
Posts: 536



Thanked: 45 times
My Contributions
Right, polymorphism and inheritance are often intertwined. Polymorphism is attained through inheritance. It is of course possible to have inheritance without polymorphism, though.
User is offlineProfile CardPM
+Quote Post

Aeternalis

RE: Polymorphism, What Is Your Understanding?

3 Aug, 2009 - 02:27 PM
Post #10

D.I.C Regular
***

Joined: 13 Jul, 2009
Posts: 261



Thanked: 25 times
My Contributions
Nice distinction LoveIsNull, It may be best, (as in most helpful to understanding Polymorphism) to talk about it in the terms of what benefits polymorphism achieves.

QUOTE

In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts.



What advantage does this give us? Well, most descriptions I have seen use it to provide a generic interface for a virtual class (in C++) This just means that it's a set of methods that can be called no matter what instance your working with... Lets say we have an Animal class, and from this class we inherit the Snake class and the Horse class. The generic interface might have a method called move() that is implemented differently in each of the inherited classes.. one causing the snake to slither along and the other causing the horse to gallop or trot..

One of the benefits of this type of implementation is that if you add another class, say..Bird there are no changes that need to be made to the interface.. the move function can still be called but the implementation will be different for an instance of the Bird class..flying obviously.

This is a benefit of polymorphism, and by describing the benefits of it.. hopefully what it is becomes a little more clear to people that are not familiar with it.

Aet
User is offlineProfile CardPM
+Quote Post

snowflakes

RE: Polymorphism, What Is Your Understanding?

20 Sep, 2009 - 10:32 AM
Post #11

D.I.C Head
**

Joined: 28 Jul, 2009
Posts: 248



Thanked: 14 times
My Contributions
poly means many

morphism means forms

polymorphism == many forms
User is offlineProfile CardPM
+Quote Post

aks29921

RE: Polymorphism, What Is Your Understanding?

20 Sep, 2009 - 11:20 PM
Post #12

D.I.C Head
Group Icon

Joined: 24 Aug, 2009
Posts: 205



Thanked: 78 times
Dream Kudos: 25
My Contributions
above understanding given by snowflakes is the simplest one...

also, i would say that polymorphism is of two types:
compile-time:fuction overloading,operator overloading
run-time:virtual functions
you can get many tutorials and snippets if you want to see how these are implemented

so actually polymorphism is a bit of everything that has been written in various posts of this forum
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:31AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month