Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try to describe polymorphism as easy as you can [closed]

How can polymorphism be described in an easy-to-understand way?

We can find a lot of information about the subject on the Internet and books, like in Type polymorphism. But let's try to make it as simple as we can.

like image 941
Moran Helman Avatar asked Oct 16 '08 22:10

Moran Helman


People also ask

How do you describe polymorphism?

In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

What is polymorphism your answer?

answer Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.<br />Any Java object that can pass more than one IS-A test is considered to be polymorphic.

What is polymorphism in oops in simple words?

Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times. Polymorphism is the ability of a programming language to present the same interface for several different underlying data types.

What is polymorphism used for?

Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.


2 Answers

Two objects respond to the same message with different behaviors; the sender doesn't have to care.

like image 150
Steven A. Lowe Avatar answered Sep 29 '22 11:09

Steven A. Lowe


Every Can with a simple pop lid opens the same way.
As a human, you know that you can Open() any such can you find.

When opened, not all cans behave the same way.
Some contain nuts, some contain fake snakes that pop out.
The result depends on what TYPE of can, if the can was a "CanOfNuts" or a "CanOfSnakes", but this has no bearing on HOW you open it. You just know that you may open any Can, and will get some sort of result that is decided based on what type of Can it was that you opened.

pUnlabledCan->Open(); //might give nuts, might give snakes. We don't know till we call it

Open() has a generic return type of "Contents" (or we might decide no return type), so that open always has the same function signature.

You, the human, are the user/caller.
Open() is the virtual/polymorphic function.
"Can" is the abstract base class.
CanOfNuts and CanOfSnakes are the polymorphic children of the "Can" class.
Every Can may be opened, but what specifically it does and what specific tye of contents it returns are defined by what sort of can it is.
All that you know when you see pUnlabledCan is that you may Open() it, and it will return the contents. Any other behaviors (such as popping snakes in your face) are decided by the specific Can.

like image 34
David Frenkel Avatar answered Sep 29 '22 11:09

David Frenkel