Aviv Cohn
Aviv Cohn

Reputation: 17193

What exactly is Polymorphism?

I have tried to understand exactly what Polymorphism in OOP means. All of the tutorials I have read say pretty much the same thing:

'Polymorphism is the fact that if class A inherits/extends class B, than an object of class A could either be referred to as an object of class Aor an Object of class B'.

Thus if in a game I have several type of missiles, and they all extend the Missile class, I can create an array of Missiles and they will all be able to be inside it, because they are all types of Missiles, even though directly they are SpeedMissile or ExlpodingMissile or whatever.

Is this the definition of Polymorphism? Is that all? It is considered a primary principle in OOP, but somehow I can't figure out why it is so important. Also, essentially it's just a consequence of the inheritance principle. Then why is it so important?

Upvotes: 3

Views: 1561

Answers (4)

Lodewijk Bogaards
Lodewijk Bogaards

Reputation: 19987

Is this the definition of Polymorphism? Is that all?

Yes, you've got it exactly right. That is all. Sometimes simple concepts have difficult sounding names. It's important not to forget this term was first coined in 1967. Meaning it was invented in a time computers were huge expensive terribly hard to understand machines that were operated on primarily by scientists.

It is considered a primary principle in OOP, but somehow I can't figure out why it is so important. Also, essentially it's just a consequence of the inheritance principle. Then why is it so important?

Let's say you wanted to fire all those missiles you were talking about. In languages that support polymorphism you can simply write for(m in missiles) m.fire(). In languages that don't you would have to fire each type of missile individually, as SpeedMissile.fire, ExlpodingMissile.fire and ScudMissile.fire would not be multiple (poly) methods of the same form (morph).

That seems important enough to me. Polymorphism is the basis for almost all OO design patterns. Without polymorphism you couldn't create a factory, visitor, strategy, adapter, visitor, etc. In short, OOP without polymorphism would suck.

Upvotes: 2

Ioan
Ioan

Reputation: 5187

I think wikipedia gives the best answer : wiki.

In a nutshel : You have more objects of the same type which behaves differently.

Type reference = new TypeOrSubclassesOfTypeObject();
Type anotherReference = new AnotherTypeOrSubclassesOfTypeObject();

In this case the objects will always be of the same type Type, but their behavior will be defined in subclasses.

Upvotes: 1

agentnega
agentnega

Reputation: 3548

Actually polymorphism is not a consequence of inheritance, and neither actually implies or requires the other. They do tend to occur together in popular programming languages, but technically the two ideas are orthogonal.

Polymorphism means that more than one type of object can be used in a similar way without knowledge of the specific type. In Java and C# the Interface is the plainest example of this idea. Notice that there is nothing inheritance-y about an Interface.

Inheritance means that the behaviour of one type of object can be automatically included into other types. Obviously the simplest example is subclassing. But isn't that automatically polymorphic? No -- if the subclass doesn't override the parent class behaviour (or the parent class doesn't allow it, i.e. in C# not marking the parent class methods virtual) then no polymorphism is actually involved.

Also see some similar questions with great answers:

Polymorphism vs Inheritance

Polymorphism - Define In Just Two Sentences

Upvotes: 1

Sameer Naik
Sameer Naik

Reputation: 1412

Shape->draw() is a polymorphic method. Its implementation changes based on the type of shape, hence the name poly=many morphic=forms. A GraphicsEditor can then draw any Shape as long as it implements draw() method.

for (shape : shapeList) {
    shape->draw();
}

In real life, anyone can drive a car as long as it has (i.e. implements) these basic operations e.g. brake(), accelerate(), turnRight() and turnLeft() and goInReverse(). How brake() method is implemented in a BMW vs Honda could be totally different but end result is same i.e. stopping the car.

Imagine the chaos if there were no standard interfaces and every car had different controls for driving.

Upvotes: 1

Related Questions