• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Generic Supertypes, page 122 (Java OCP 8 Programmer II Study Guide, Sybex)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky,

On page 122, in sidebar "Understand Generic Supertype" - on line 3 an ArrayList of type Exception is created. When the ArrayList type is Exception and object being added is Exception on line 4, why doesn't it get compiled?

As per List declaration, ArrayList type could be IOException or super type.

Thank you in advance.

Vinod Pothuru
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinod, can you quote the related code snippet? More people will be able to help you if you do, not all of us have the book.
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the study guide, so let me provide the appropriate code snippet
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinod Pothuru,

First of all, a warm welcome to CodeRanch!

Vinod Pothuru wrote:When the ArrayList type is Exception and object being added is Exception on line 4, why doesn't it get compiled?


Because the compiler does not know about the ArrayList object at the right hand side. Remember the compiler does not execute any code! So the compiler only knows about the type of the reference variable exceptions (which is List<? super IOException>). And the compiler is a real smart cookie and he knows, this type declaration allows the following ArrayList objectsBecause the compiler only knows about the type of the reference variable, the compiler will only allow adding elements which can be added to all these lists. If an element can't be added to one of these lists, it will result in a compiler error when you try to add it to exceptions. So ilet's see what happens if we try to add a new Exception instance (as in line4) to any of these listsline1 results in a compiler error and that's why adding a new Exception instance (as in line4) to exceptions is not allowed and will result in a compiler error. Let's do the same thing with an IOException (as in line5)All these lines compile successfully and that's why you can add a new IOException instance (as in line5) to exceptions without getting a compiler error. And of course the same applies to FileNotFoundException.

Hope it helps!
Kind regards,
Roel
 
Vinod Pothuru
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan van Hulst: Thank you for your immediate attempt to help me.

@Roel De Nijs: Thank you for warm welcome and nice explanation with examples.

Upon reading other materials and the book again, I also figured out that upper bound wildcard generic collection reference becomes immutable because of possible runtime exceptions. But when it comes to lower bound wildcard generic collection, it is possible to add same or subclass type objects of generic collection reference type. The collection reference variable can be specified generic type or super type, while the objects in the collection can be specified generic type or subclass type.
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Pothuru wrote:Upon reading other materials and the book again, I also figured out that upper bound wildcard generic collection reference becomes immutable because of possible runtime exceptions.


This is not quite correct.
It is true that you cannot add any elements to an upper-bounded collection, but do not mistake that for immutability. The collection referred to might still be very much mutable.
You could, for instance, still call clear() on such a reference. Or another reference to the same collection could have different variance and thus be able to add elements to the collection.
 
Vinod Pothuru
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. Thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic