Swapna latha wrote:
Paul Clapham wrote:
Swapna latha wrote:
This is a List of some unknown class which is a superclass of IOException. Let's call that class X (the unknown). Then X could be IOException, or Exception, or Throwable, or Object. In particular X might be IOException, so adding an Exception object to the list would be wrong.
People often think that a List<? super IOException> can contain any object whose type is a superclass of IOException. That isn't the same thing and it isn't how Java handles it.
Or you might think that because you assigned a List<Exception> to that variable, that means something to the compiler. But it doesn't -- the compiler must ignore that because your code could subsequently assign a different value to the variable.
Swapna latha wrote:I havenot understood clearly. As per Kathy Sierra and Bert Bates ocp book , ? extends IOException means anything that extends IOException , so we can have even an Exception right. Why Java is not allowing it ? Am i wrong in understanding the concept. Whats the use of Right hand side of the expression which has generic type of Exceptionn .
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:
Swapna latha wrote:I havenot understood clearly. As per Kathy Sierra and Bert Bates ocp book , ? extends IOException means anything that extends IOException , so we can have even an Exception right. Why Java is not allowing it ? Am i wrong in understanding the concept. Whats the use of Right hand side of the expression which has generic type of Exceptionn .
Your original question is about ? super IOException, ? extends IOException is a similar case but not exactly the same. Let's look at both cases:
List<? super IOException> means the List can be of type IOException or its ancestors. Note here that List can actually be of type IOException as well. So if Java compiler allows you to add Exception to this list, it would break the type safety as you can see in the example below:
Now let's consider the ? extends IOException case. In this case, the List can be IOException or any of its descendants. If the List is actually of some sub-class of IOException, and compiler allows you to add IOException to the list, the following code will break:
Thus add operation is not allowed in either case. If you do a get operation however, you'll see different behavior. In case of ? super IOException, the List could actually be of type Object as well. But in case of ? extends IOException, you know that the elements in the List are compatible with IOException at least. So the get operation would work like this:
Swapna latha wrote:I got a program where it says "? super String" and we can add even object to it.
Paul Clapham wrote:
Let's use a simpler version of what you wrote. Now, which of those lines of code compile and which don't?
Keep in mind that the compiler only knows the type of the variable which refers to the list object. It doesn't know the type of the object itself.
Swapna latha wrote:Thanks for making me to understand with the help of examples.
But why does not superString add new Object as the generic type says "? super String" and Object is super of String.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Paul Clapham wrote:
Swapna latha wrote:Thanks for making me to understand with the help of examples.
I'm happy to hear that you understand the concept now.
Ankit Garg wrote:
But why does not superString add new Object as the generic type says "? super String" and Object is super of String.
Are you still confused? If you are let me try another example
Let's say I'm writing this API for anyone to call. The second call doesn't compile. Why? Because there is no guarantee that the List I'm passed can store Object. If the compiler allowed me to add Object to this List, it can break type safety. What if I wrote the following code to call this method:
If the manipulateList method was allowed to add Object to stringSuperList, the above code will end up having an Object in a String list. An Object is-not-a String. If somehow I'm able to add Object to it, when I retrieve the element, I'll get ClassCastException. Generics is supposed to save you from ClassCastException, not let it happen.
Ng Sharma wrote:Best Answer.
https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|