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 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. 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.
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.
Damn it. I totally didn't see this thread before I made mine
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.
Right, polymorphism and inheritance are often intertwined. Polymorphism is attained through inheritance. It is of course possible to have inheritance without polymorphism, though.
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.
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