Regards, Abdul Mohsin
public static <T super Number> void hello(Collection<T> aa){ } is not valid why?
cmbhatt
Regards, Abdul Mohsin
you mean to say only because of "tiny use" sun make it compiler error
cmbhatt
Regards, Abdul Mohsin
Swarna says,
public static <T super Integer> void hello(java.util.Collection<T> aa){ } //Compiler error
public static void hello(java.util.Collection<? super Integer> aa){ }
//compiles fine
Type erasure:
Colletion<? extends Number>
will become
Collection<Number>
Collection<? super Number> will become
Collection<Object> after type erasure by the compiler.
cmbhatt
cmbhatt
Value-add of line 1 or line 2 (if it was allowed) would be ability to add an object of type Integer to the Collection.
Cited reason
(1-) restriction in creating object of that class
Wouldn't the same restriction apply to line 1?
(2-) Only allowed to access methods of the lower bound classes Object.
Wouldn't that be the case for line 1 as well?
And finally when the type erasure makes it
void hello( <Collection<Object> c) {}
And finally , what would be the type erasure for the line 1?
cmbhatt
cmbhatt
Case 1>
public static void hello(java.util.Collection<? super Integer> aa){ } //line 1
This is ordinary method takes a Class Object which has been implemented as a generic class.
After Type Erasure becomes
public static void hello(java.util.Collection aa){ } //Nothing will remembered
CASE 2>
public static <T super Integer> void hello(java.util.Collection<T> aa){ } //line 2
This is ofcourse comes under "generic method declaration" but its wrong.
For generic methods T will be replaced with compatible Object depending on its filter<T super Integer> in "Type Erasure" process.
Since your filter is <T super Integer>
it has to replace Collection<T> with Collection<Object> ( i will tell
you why Object next)
But generic stuff can't remembered at Runtime.
Hence you will be stopped on this stamtement.
***
Generic methods will include direct type. like
public static <T super Integer> void hello(T aa) { } //line 2
This is fine. After type Erasure it will become
public static void hello(Object aa) { } //line 2
But there is no need to have a method like that with Object
Hence usage of T super XXX has been ruled out.
Why menas hello(Number) is valid call but it can't make use of its
methods in hello bec'ze we are passing it to Object. ------------------------------------------------------------------
Why Object has been replaced for T in this example ???
For <T super Integer> JVM has to select upper bound which will take
any super class Integer!.
Object is only choice which can accomodate any class which is
"super to" Integer or any other.
So <T super xxx> will always be replaced(T Arguemnts) with Object.
(So its useless to have function like this which will accept Object)
-------------------------------------------------------------
<T extends Car> is valid in generic methods. Why ?
Lets see what happens with <T extends Car>
public static <T extends Car> void hello(T aa) { } //line 2
//After Type Erasure becomes
public static void hello(Car aa) { } //line 2
This is so simple.
<T extends Car> means any class that extends Car is valid.
So as Car can hold any of its Childs like Benz,BMW,Lancer..
So the generic notation would be
before erasure <T extends xxx>
After erasure argument T replcaed with xxx.
----------------------------------------------------------------
***
Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Consider Paul's rocket mass heater. |