This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
In the program i am creating an instance for GenClass with generic type as B,since generic type in GenClass can take anything that extends A ,it is ok to create an instance with generic type with B.Infact i canpass the generic type as A or B or C.
But my doubt is when invoking the method i pass the reference to a type A<Integer> so i thought the generic type T inside the method would be considered as Integer,so that the method will throw error.I thought since T is considered as Integer then T extends E would fail ,but the following method compiles successfully.
But if I am returning anything that is not A or super of A then it is showing the compilation error.
I do not understand the relation between these fragment T extends E and the return type ? super T and the argument ? super E.Are they not relate to each other?
Siva Masilamani wrote:But my doubt is when invoking the method i pass the reference to a type A<Integer> so i thought the generic type T inside the method would be considered as Integer,so that the method will throw error.I thought since T is considered as Integer then T extends E would fail ,but the following method compiles successfully.
I agree. It should not compile for the reason given. Wait a sec... Just tried it. And it did not compile.
it compiles fine (notepad & dos) jdk 1.6
there are two views to solve this type of problem
1 view>
paramether passing must be same as argument
A<Integer>a=new A<Integer>();
parameter a i.e A<Integer>
argument A<? super T> //anythig whos super is T
2 view>
so what is T ?
see left <T extends E>
so what is E ?
A<E>
again what is E?
in main
we wrote that
A<Integer>a=new A<Integer>();
-------------------------------------------
so Integer Integer matches
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
posted
0
[quote=rutuparna andhare]it compiles fine (notepad & dos) jdk 1.6
there are two views to solve this type of problem
1 view>
paramether passing must be same as argument
A<Integer>a=new A<Integer>();
parameter a i.e A<Integer>
argument A<? super T> //anythig whos super is T
2 view>
so what is T ?
see left <T extends E>
so what is E ?
A<E>
again what is E?
in main
we wrote that
A<Integer>a=new A<Integer>();
-------------------------------------------
so Integer Integer matches
[/quote]
E - Generic type is not from class A instead from class GenClass and has no relation to A.
This code should not be compiled as it is.
But some IDE especially Netbeans compules fine but Eclipse shows error.
rutuparna andhare
Greenhorn
Joined: Oct 29, 2009
Posts: 14
posted
0
i respect everyones thought
i think this will help
i think E is confusing ! right
public <T extends E> A<? super T> getNow(A<? super T> a)
in this sentance whatever is in argument A<----T---> relates to leftside <T extends E> but only and only for class "A"
to check this you can
compile this code
--------------------------------------------------------------
public class TestGeneric
{
public static void main(String [] args)
{
A<Integer> a = new A<Integer>();
new GenClass<B>().getNow(a);
}
}
class A<E>
{
static void call()
{
System.out.println("in class A");
}
}
class B extends A
{
static void call()
{
System.out.println("in class B");
}
}
class C extends B
{
static void call()
{
System.out.println("in class C");
}
}
class D { }
class GenClass<E extends A>
{
public <T extends E> A<? super T> getNow(A<? super T> a)
{
E.call(); // shows what type of class E is
return new A<A>();
}
static void call()
{
System.out.println("in class GenClass");
}
}
------------------------------------------------------------------------------------
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
posted
0
in this sentance whatever is in argument A<----T---> relates to leftside <T extends E> but only and only for class "A"
Could you please explain this more elaborately?
i am soo confused now.
Then why different IDE gives different result?
Which is one is corrct?
I am being struck in this for past 3 days and i am afraid that my SCJP exam is on Nov 14th