File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in generic method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in generic method" Watch "Doubt in generic method" New topic
Author

Doubt in generic method

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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.

SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
please somebody help.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
It compiles and runs fine for me.

I am using Netbeans IDE with jdk 1.6

still confused???
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Sep 19, 2008
Posts: 385
Henry,Leandro

Are you able to find the reason why it compiles ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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

Joined: Sep 19, 2008
Posts: 385
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

Joined: Oct 12, 2009
Posts: 24
It compiles on my machine. I use dos and jdk1.6.3.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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

Joined: Oct 29, 2009
Posts: 14
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: 385
[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
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: 385
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt in generic method