• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic doubt

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why I would get a compiler error (incompatible types) when trying to past an ArrayList into a method that excepts a List. An ArrayList is-a List.
I can do this: 'List<String> list = new ArrayList<String>();
So why can't I do it with the method argument?



Thanks,
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Jeff Schuler


How can you assign a super class reference to a sub class reference ?

Here ArrayList is-A List


Have you Got it ?


Thanks

Anil Kumar
[ July 02, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is not with the argument. it is with the return type. It expected a List but found ArrayList which is a subclass. You can typecast it.

cheers,
nitin
 
Jeff Schuler
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anil kumar:
Hi

Jeff Schuler


How can you assign a super class reference to a sub class reference ?

Here ArrayList is-A List


Have you Got it ?


Thanks

Anil Kumar

[ July 02, 2007: Message edited by: anil kumar ]



Thanks, I got it. I have to start reading the questions more carefully.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic