• 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

Java Generics

 
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Supposedly we have a class :

Node <E> {}

For subtype covariance ..

why Node<Integer> is a subtype of Node <? extends Integer> and not the other way round?

Since Node<? extends Integer> has the max upper bound of Integer so all subtypes (which is ? extends Integer> ) will always be subtype of Integer?

So confusing

 
Sheriff
Posts: 9708
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

Kim Ming Yap wrote:why Node<Integer> is a subtype of Node <? extends Integer> and not the other way round?



I think you are talking about this



The first one doesn't compile because there is a chance that type safety is broken by that assignment. Let's take a different example to understand this as Integer is a final class so cannot be sub classed



The InputStream class has many sub-classes. Let's consider FileInputStream and PipedInputStream. Now if my code is like this



Now if the 4th line in the above code was allowed, then it can break type safety as follows



I hope you understand it now...
 
reply
    Bookmark Topic Watch Topic
  • New Topic