What is polymorphism? Explain with an example?  

"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.  

Showing Answers 1 - 7 of 7 Answers

Zaid

  • Mar 27th, 2005
 

An example would be: 
A "vehicle" class can be a part of a inheritance heirachy where derived classes are "sedan" and "SUV". Derived from "sedan" could be "Corolla", and derived from "SUV" could be "Pathfinder". 
 
CAR 
/  
SUV SEDAN 
/  
PATHFINDER COROLLA 
 
 
Now, using such an example, it is true that any object below in a heirarchy is also something that is directly up in the heirarchy. Hence, Pathfinder "is a" SUV, and SUV "is a" CAR. Also, Pathfinder "is a" CAR. 
Hence, using pointers of Base classes (higher in an inheritance heirarchy) can be assigned to objects of derived classes and can be used in a unified manner with the use of virtual functions. Hence, Polymorphism. 
 
(The plus "+" operator example used above would not be correct, as that is actually an overloaded operator (in the case the last poster presumed) and not precisely polymorphism).

sanneboyina

  • Feb 16th, 2006
 

A

  Was this answer useful?  Yes

Polymorphism is "acquiring many forms". as it has been stated that c++ has the property of polymorphism.Like as someone said the eg of vehicle...it can be said that the particular operation can acquire one or many forms

  Was this answer useful?  Yes

vsridevi

  • Sep 16th, 2008
 

C++ enables polymorphism - The ability of obejcts of different classes related by inheritance respond differntly to the same function call.

  Was this answer useful?  Yes

MCBod

  • Mar 31st, 2010
 

Polymorphism is a standard OO principle whereby one or more "sibling" classes may be defined as being children of some parent class, and where these classes provide some different implementations for the same method(s)
The Polymorphic concept is related to the late binding whereby a reference can be made to an instance of any one of these children, in the form of a reference to the parent class. Only at runtime does the system identify which instance of the method should be executed based on the actual class type being referenced

  Was this answer useful?  Yes

puzzlu

  • Sep 27th, 2010
 

The primary usage of polymorphism is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding).

Polymorphism is not the same as method overloading or method overriding. Polymorphism is only concerned with the application of specific implementations to an interface  or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves implementations of polymorphism.

  Was this answer useful?  Yes

puzzlu

  • Sep 27th, 2010
 

In object-oriented programming, polymorphism is a generic term that means 'many shapes'. (from the Greek meaning "having multiple forms"). Polymorphism is briefly described as "one interface, many implementations."
Polymorphism is a 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.

There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions. Here are some ways how we implement polymorphism in Object Oriented programming languages.

Compile time polymorphism -> Operator Overloading, Function Overloading
Run time polymorphism -> Interface and abstract methods, Virtual member functions.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions