• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in generic method

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please somebody help.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles and runs fine for me.

I am using Netbeans IDE with jdk 1.6

still confused???
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,Leandro

Are you able to find the reason why it compiles ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic