Skip to main content
GameDev.net gamedev.net
🔒 Locked

What is Polymorphism

Started by valsar Feb 26, 2008 at 12:40 AM 1 replies 1.2k views
Original Post
valsar
valsar
Basically what the title says. I'm learning C# using the Deitel book and I'm having a hard time understanding what polymorphism is and how it works. Could someone please help me understand what's going on and maybe use some easy to understand examples so I can see how it works as well. Thanks.
Shiny
Shiny
Wiki: Polymorphism.

If that doesn't help, think about it this way; when you're programming, you're dealing with a bunch of logical constructs -- some of these, especially in C# -- can be referred to as Objects. Now, objects have, depending on which language they manifest themselves in, different properties -- some of these may be methods/functions that belong to the object -- others might be data or fields that belong to the object. What happens if you have some object that represents a Vehicle in your game? Vehicles have some common attributes; location, perhaps the velocity at which they are travelling, what colour they are, etc.

Now say you want to create a 'Car' object that is identical to the 'Vehicle' one, except that you can 'drive' it using a method named 'drive' -- it would be awful if you had to re-write all the code for Vehicle when Car is so closely related...hence polymorphism -- you can derive (inherit) the functionality of 'Vehicle' into your new 'Car' -- depending on how you declared the methods and attributes of your Vehicle, you can call the methods belonging to Vehicle from your Car class and save a bunch of code.

Obviously, this is not always what we want -- maybe you want to be able to customise what happens when you call a certain method from parent class Vehicle -- so you can do things like -override- the behaviour that would normally happen, and provide new behaviour that suits the derived class. Polymorphism is discussed in familial terms quite often -- so, you have a parent class, and then you might have its child classes that inherit some behaviour. The trick is to know which is which, and who has access to what.

I think p'raps I've rambled a bit much -- your book ought to be more concise than this -- and Wiki is for sure! I'm not going to provide code examples -- your book can do that; if you have any more questions, fire away :)

Note: Heuristic for remembering who gets access to what in C# -- child classes inherit public fields and methods from their parents, and also have access to 'protected' ones too. Anything declared with private in front is invisible to children.


~Shiny.
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup
Oluseyi
Oluseyi
Poly - many, multiple
Morph - type, kind or form; ability to change type, kind or form
-ism - suffix used to form a noun of action from a verb

Polymorphism - the state or property of being able to change/assume many types, kinds or forms. In programming terms, that is the ability for an object or interface to appear to be of different types at different times, as needed.

The great problem most beginners have with polymorphism is that the examples used to illustrate it are typically silly - why would I want to make a Mammal class of which Cow is a specialization that implements the speak() function by printing Moo?! The reason the examples given are silly is because useful examples are more complex and require you to have learned more basic concepts first. This could trivially be remedied by postponing the introduction of polymorphism.

Right now, you don't need to write polymorphic classes. The only thing you need to know is that classes are sometimes related, such that you can ignore which precise type they are. This lets you use polymorphically implemented standard library objects, but doesn't bog you down in needless theory. Later, when you're trying to figure out the sorts of challenges for which polymorphism was developed, you can be introduced to it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.