GeekInterview.com
Series: Subject: Topic:
Question: 260 of 268

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.  
Asked by: Interview Candidate | Asked on: Mar 27th, 2005
Showing Answers 1 - 7 of 7 Answers
Zaid

Answered On : 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).

Yes  1 User has rated as useful.
  
Login to rate this answer.
sanneboyina

Answered On : Feb 16th, 2006

A

  
Login to rate this answer.
nitin_chawda

Answered On : Apr 15th, 2006

View all answers by nitin_chawda

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

  
Login to rate this answer.
vsridevi

Answered On : Sep 16th, 2008

View all answers by vsridevi

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

  
Login to rate this answer.
MCBod

Answered On : Mar 31st, 2010

View all answers by MCBod

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

  
Login to rate this answer.
puzzlu

Answered On : Sep 27th, 2010

View all answers by puzzlu

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.

  
Login to rate this answer.
puzzlu

Answered On : Sep 27th, 2010

View all answers by puzzlu

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.