Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

A question on Generics

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone please explain why the following code works?



Something strange is going on here. I don't really understand how FileNotFoundException or EOFException can be added to the list even though they are not super type of IOException.

I appreciate your help.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the ArrayList reference can take any generic type that is a supertype of IOException.

So the reference could be an ArrayList<Exception>, and it could be an ArrayList<IOException>, and it could be an ArrayList<Object>

So if you assume that the Generic type of the ArrayList MUST be IOException or a supertype of IOException...
which types is this ArrayList guaranteed to be able to take as parameters?
 
Suman Sarker
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:So if you assume that the Generic type of the ArrayList MUST be IOException or a supertype of IOException...
which types is this ArrayList guaranteed to be able to take as parameters?



Thanks for your reply. I would expect it to take an Exception or even an Object as its parameters but not somthing that's subtype of IOException. I thought the super wildcard is there to impose a restriction on the list to not allow any sub types of IOException. FileNotFoundException and EOFException are subtypes of IOException. Aren't they?

For example, the following code doesn't work and I cmpletely understand why it doesn't.



So, I still fail to understand why the code in my first post works.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The super wildcard is there to guarantee that you do not pass a List with a Generic type that is a subtype of IOException.


Which means you could not do this:

List<? super IOException> arrayList = new ArrayList<FileNotFoundException>();

this will not work.
Now, this means the compiler just made sure that the generic type of the list you're passing is IOException or a superclass of IOException
And since we know that we can assign any object to a reference of its supertype in Java (for instance, a List<Exception> can take as a parameter in its add() method an Exception or any of its subclasses)...
What did the compiler just make sure?
 
Suman Sarker
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got ya. Thanks once again for your quick reply.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got another one for you;

What could you pass to the List if the reference type was "List<? extends IOException>"?
 
Suman Sarker
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:Got another one for you;

What could you pass to the List if the reference type was "List<? extends IOException>"?



A List with any class that that extends IOException?

For example, ArrayList<FileNotFoundException> should work this time.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what could you add to that list?
 
Suman Sarker
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:And what could you add to that list?



I don't think extends wildcard would let you add "anything" to the list. That's something I already know ;)
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotta love that null reference

Good luck on the exam!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good help Dieter!
 
Suman Sarker
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic