• 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

Generics doubt?

 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone tell me why line 2 compiles file ,but line1 does not-
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics needs to be backward compatible -- so line 2 is allowed. It does however, generate warning errors. And the assignment is not recommended.

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

Line 1 doesnt compile because list1 is declared as of type List<Object>.
So list1 can be assigned only to a List or subtype of List having Object as Generic parameter.

Here in Line1, list1 is being assigned to an object of ArrayList<Integer> which is not legal. Thats why the code doesnt compile.

Hope this is clear!
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If line 1 would have compiled, then it would have removed the type safety that generics provide.

Suppose the following line would have compiled
ArrayList<Object> l = new ArrayList<Integer>();

then you could have done this
l.add(new String("hello"));

as l is of type Object and String is a sub-type of Object. Then look at the following code



So as you can see that in that case, there would be a run-time exception. This is what we are trying to avoid using Generics...
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This like assuming a basket of apples as a basket of fruits. Although intutively correct, this is going to break if you try to put a mango in the apple basket thinking it was a fruit basket.
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha ha Satya Nice example
 
Ashok Pradhan
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK,Thanks to all
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice Example Satya.

Ananth Chellathurai
 
reply
    Bookmark Topic Watch Topic
  • New Topic