Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have clear Idea about the term polymorphism still I am curious about generics. Can we treat the generics as polymorphism or not? It would be helpful if any link stating the same can be shared along with the views on question.
Posted
Comments
David Lee 145 24-Aug-14 21:28pm    
I think we cannot treat the generics as polymorphism. In my opinion the best case of polymorphism is the factory pattern.

See this page.
http://www.dotnetperls.com/factory
Sergey Alexandrovich Kryukov 24-Aug-14 23:19pm    
You are right, but this not related to the notion of factory.
Please see Solution 2 where I tried to explain why we cannot consider generic-based polymorphism. Generic type simply do not exist as the runtime types of any objects. Object types are always non-generic.
—SA

No, we cannot.

Let's suppose we have some set of objects; and some or all of those objects have been created by instantiation of some generic classes. Let's look at the runtime types of those objects. All the types are not generic, because, before we can instantiate an object, we needed to instantiate a generic type to become a complete type, not a generic type. In other words, run-time types of objects are never generic. This way, we can exclude this feature from consideration when discussing polymorphism.

—SA
 
Share this answer
 
v2
Comments
David Lee 145 24-Aug-14 23:43pm    
Yes. You explained the reason very well.
Sergey Alexandrovich Kryukov 25-Aug-14 0:01am    
Thank you, David.
—SA
Abhinav S 25-Aug-14 0:05am    
Well explained.
Sergey Alexandrovich Kryukov 25-Aug-14 0:05am    
Thank you, Abhinav.
—SA
Generics and polymorphism can be made to work together - http://msdn.microsoft.com/en-us/library/vstudio/dd799517%28v=vs.100%29.aspx[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Aug-14 23:17pm    
Yes of course, but you completely avoided answering the main question: can generics make kind of polymorphism or not?
I tried to answer, please see Solution 2.
—SA

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