Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in generic method

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Please consider the below code.



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?

please help me.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please somebody help.
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles and runs fine for me.

I am using Netbeans IDE with jdk 1.6

still confused???
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compile and runs fine.
I also have the same doubts. It's a strange code.

E -> B
? -> Integer
T -> ???

What T would be?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,Leandro

Are you able to find the reason why it compiles ?
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:Henry,Leandro

Are you able to find the reason why it compiles ?


It did *not* compile for me. I tested it with the Eclipse Java 5 compiler.

Henry
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry

I checked the code in Eclipse and it shows error.

But why i am able to return only A or Super of A i.e Object not B or C.

I have passed the parameter to the function as A<C> a=new A<C>.(Replaced Integer as C)
 
zhong chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles on my machine. I use dos and jdk1.6.3.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you use Eclipse or Netbeans?

I tried with WSID 6.2 with default JRE and it did not compile which is correct.If it compiles then it is wrong.

I think this is a bug with in Some JDK version.

I use JDK 1.6.16 in Netbeans
 
rutuparna andhare
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Many thanks in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic