Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
is polymorphism is allowed to used with generics ? if yes can give me a real example real code used polymorphism and what is the difference between ArrayList and Array when we talk about Polymorphism
thanks
Posted
Updated 26-Dec-11 21:12pm
v3

Polymorphism is not a phenomena which can be allowed or prohibited. Generics cannot change the situation, because they have nothing to do with polymorphism, because they have nothing to do with inheritance, "virtual" and late binding. Generics are static; any type which can be instantiated during run-time is not generic; it is instantiated into a final type before an instance (object) is constructed. Generic types and final types created out of generics are always the compile-time types. In contrast, all "virtual" behavior and late binging is played on instances and their run-time types, during run-time.

This way, generics and polymorphism are parallel phenomena which do not intercept; therefore, they can be freely combined in any thinkable ways.

A code sample? There are too many samples around.

Here, you can find a conceptual example, good for nearly all object-oriented language: http://en.wikipedia.org/wiki/Subtype_polymorphism#Examples[^].

See also http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

Why knows how many samples you can find in tutorials:
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html[^],
http://home.cogeco.ca/~ve3ll/jatutor5.htm[^],
http://www.javaworld.com/javaworld/javatips/jw-javatip30.html[^],
http://www.javaworld.com/javaworld/jw-04-2001/jw-0413-polymorph.html[^].

After all, Google for more.

—SA
 
Share this answer
 
Comments
Espen Harlinn 27-Dec-11 11:30am    
Good reply - a 5 as usual :)
Sergey Alexandrovich Kryukov 27-Dec-11 11:36am    
Thank you, Espen.
How have you been lately?
--SA
Espen Harlinn 27-Dec-11 12:04pm    
Extreemly busy :(
Dear Hamzah,

Please follow this link it might help you out.

http://stackoverflow.com/questions/1814851/c-sharp-generics-and-polymorphism-an-oxymoron[^]

Thanks
 
Share this answer
 
ye you can create overload of a method
Eg:
public void GetSomething(T a)
{
do something
}

public void GetSomething(T a,K b)
{
do something
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900