• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Polymorphorphic reference ?

 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can anybody suggest why we use like



instead of




One reason is it will restrict to access the methods in the List interface ...


Thanks ...!!!

Regards,
Chetan
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because someday, you may want to use (for example) a LinkedList instead of an ArrayList.

Further, it is possible you may not even know the specific type of the object you get. If you use code from a 3rd party, you may just be given a static method called "getList()". You won't know if it is an ArrayList, a LinkedList, a FredsNewAndImprovedList, or what.

By using the interface reference, you don't NEED to know. Your code will work regardless of the specific subtype.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chetan Dorle wrote:Hi All,

Can anybody suggest why we use like

. . .

I would hope you don't use that, butYou can only use <> (use ctrl-F→"diamond") in Java7; before that you had to repeat the actual type parameter like this: <Foo>.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above example does not make sense enough, I think.
Consider this example:



The caller can pass different kinds of List (i.e ArrayList, LinkedList...) into the method's parameter. The method's body does not care about what the real object is, it assumes the thing passed is a kind of List. This make flexibility for callers who may construct different kind of List as per their need.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks fred & Minh,

You clicked my problem correctly....

Regards,
Chetan
 
reply
    Bookmark Topic Watch Topic
  • New Topic