what is polymorphism with example?types of polymorphism?
Answers were Sorted based on User's Feedback
Answer / ramaraju
polymorphism means "same thing will exists with different
forms"
Ex :suppose we need to find volume of
circle,rectangle,triangle.ect in a sampe program.
genrally what we need to do is write the code volume for
cirle,rectangle,triangle in sepratly.
using polymorphism concept we simply write the volume code
with different parameter list ect
Ex:
class a {
volume(int a)//for rectangle
{
---
}
volume (int a,intb,intc)//for triangle
{
--
}
volume (string s)//for circle
{
--
}
}end of class A
polymorphisam are mainly two types
static polymorphisam(corresponding method will bind at the
time of compiling)
dynamic polymorphisam(corresponding method will bind at the
run time)
| Is This Answer Correct ? | 162 Yes | 18 No |
Answer / nikunj b patel
same name multiple form is call poly...
static polymorphism and dynamic polymorphism.
| Is This Answer Correct ? | 71 Yes | 13 No |
Answer / amr
well Polymorphism means one name implies many forms , which introduces generic programming . Polymorphism happens in the inheritance hierarchy , so that low level abstractions which extend higher level abstraction can override
(re-implementing a method of superclass in a subclass with identical signature) the implementation of the higher level abstraction .
example :
class vehicle {
void accelerate()
}
class car extends vehicle{
void accelerate()
{
S.O.P("um a car");
}
}
class bike extends vehicle{
void accelerate()
{
S.O.P("um a bike");
}
}
now bike and car vehicles .
so we can say
Vehicle[] v = new Vehicle[2];
v[0]= new Car();
v[1]=new Bike();
so here i have array of vehicles
i can do the following (generically )
for (i=0;i<v.length;i++)
v[i].accelerate();
-------
output:
um a car
um a bike
like i say ok guys all of u r vehicles so all of u can accelerate so do it everyone on his own way .
now that was a very polymorphic piece of code
because every time i call the method accelerate on an object the JVM will do Dynamic resolution and invoke the corresponding method of that object .
so not only overriding is polymorphic but we should keep a consistent inheritance hierarchy or abstraction levels .
---
method overloading is another form of polymorphism but its easier to implement because at compilation time all the method calls are resolved .
for ex
-----
class Add {
int add(int a, int b)
{
return a+b;
}
float add(float a,float b)
{
return a+b;
}
}
so be careful with overloading because methods are identified by it signature which is
-return type
-name
-parameters number
-parameters types
-order of parameters
since overloading means the same name then we are left with
-return type
-parameters number
-parameters types
-order of parameters
now lets see this code
Add a = new Add();
int x =a.add( 2 , 3 );
float y = a.add( 2.2, 3.4);
-----------
now i guess this covers the polymorphism types
what do u say
| Is This Answer Correct ? | 39 Yes | 16 No |
Answer / hassan arafat
Polymorphism allows an entity (for example, variable,
function or object) to take a variety of representations.
Ad-hoc polymorphism: This polymorphism let a function to
have different implementations based on its parameters and
return type. Ad-hoc polymorphism is supported through
function and method overloading.
Parametric polymorphism: lets you write a piece of code
that is not associated with a particular type and therefore
can be used with any type. Object oriented languages like
C# achieve it through generics.
Inclusion polymorphism: let a type hold instances of many
different types as long as the types are related by some
common parent class. Object oriented languages like C#
achieve inclusion polymorphism through inheritance.
| Is This Answer Correct ? | 33 Yes | 11 No |
Answer / ramaiah.teepalapudi
polymorphism is the phenomenon where the same message sent
to two different objects produces two different set of
actions. Polymorphism is broadly divided into two parts:
Static polymorphism – exhibited by overloaded functions.
Dynamic polymorphism – exhibited by using late binding.
| Is This Answer Correct ? | 29 Yes | 7 No |
Answer / nidhi joon
The ability to take more than one form.
Supports two type
Method Overloading
Method Overriding
| Is This Answer Correct ? | 58 Yes | 38 No |
Answer / vignesh,c
poly means many morph means shapes so, one function taking
many shapes ,for ex: take the function name as add,
by passing different sets of arguments it can add 2no.s,3
numbers etc....
there are two types of polymorphism namely,
runtime polymorphism,compile time polymorphism,
compile time Polymorphism means function overloading,
run time polymorphism means virtual functions.
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / prashantshukla
the types of polymorphism
compile time
run time
| Is This Answer Correct ? | 5 Yes | 1 No |
Answer / vignesh,c
poly means many morph means shapes so, one function taking
many shapes ,for ex: take the function name as add,
by passing different sets of arguments it can add 2no.s,3
numbers etc....
there are two types of polymorphism namely,
runtime polymorphism,compile time polymorphism,
compile time Polymorphism means function overloading,
run time polymorphism means virtual functions.
| Is This Answer Correct ? | 3 Yes | 0 No |
What is the difference between object oriented programming language and object based programming language?
What does indexof mean?
Is map ordered in java?
What are the important features of Java 8 release?
Can multiple catch statements be used in exceptions ?
What happens when a thrown exception is not handled?
What are the differences between string, stringbuffer and stringbuilder?
What is the difference between the size and capacity of a vector?
What are the types of classes in java?
What is time complexity algorithm?
How to sort double array in java?
What is the immediate parent class of the Applet class?