aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic type doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic type doubt" Watch "Generic type doubt" New topic
Author

Generic type doubt

Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22


The options are:
1. return e
2. return e2.getE()
3. return e2
4. return e.getE()

Option (1) and (2) compile fine. Option (3) and (4) give compile error.

1. Precisely, for e2 says,incompatible types. Found: GenericDemo<E>. Required: E.
My doubts is that, for e2, isn't E that is required, part of GenericDemo<E> which is e2's type ?

2. For return e.getE() says, incompatible types. Found: GenericDemo. Required: E. Here I'm first confused as to how the return type is determined for a method invocation. getE() according to method signature returns E. But invoking it through e.getE() returns GenericDemo?? How is that happening? And even variable e is of the type E? So where is GenericDemo coming from when compiler says found: GenericDemo?

3. And how is option(2) e2.getE() returning E that is actually compiled fine?

I hope I'm able to convey my doubts correctly.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775





But in this case, the Dog passed to doStuff does not match with what e.getE() returns, which is a Cat.
The compiler is designed in this way, so that it won't return a wrong type.

Does it make sense?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
In the other words, the compiler looks at the Animal class, it does not know what E type will be. It can be Animal<Cat>, Animal<Dog>,Animal<Fish>....
When the compiler looks at the doStuff, it does not know what E e parameter will be.
The compiler only knows you will give Animal a E type, pass the same E type to doStuff, return E from getE().
But the compiler won't know what E e you will pass to doStuff.
You can pass a Dog to Animal<Cat>. The compiler will only return a Cat if you have an Animal<Cat>. If you pass a Dog to doStuff, the compiler is not "happy".
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Hello both Himai and Rahul , Im jumpin in due it's interesting question that I also don't get it at all

Going to borrow Himai's example, whole quote :

Himai Minh wrote:




But in this case, the Dog passed to doStuff does not match with what e.getE() returns, which is a Cat.
The compiler is designed in this way, so that it won't return a wrong type.

Does it make sense?


ok, I get and know why anwers 1,2 and 3. Yet I do not see answer 4.

Sticking with example, why



return is Cat type? (As Rahul, I don't get the part in from which comes "GenericDemo" type when it's expecting anything , "E" )

Thanks in advance.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
Hi, David buddy,
Why

Because E in Animal<E extends Animal> is bound to Cat.

Maybe, my Cat and Dog example was not a good one. Let me explain the whole problem in another way:


In this example, GenicDemo genDemo is not bound to any generic type. The "E" is GenericDemo type and "genDemo.getE()" returns GenericDemo type.

In this :

What is E? GenericDemo, the non-generic type. Of course, it can be any subtype of GenericDemo.
What is the return type of e.getE()? GenericDemo.
What is the return type of doStuff? E.
Is GenericDemo the same type of E? Not necessary.
For example, is Animal the same type of Cat? Not necessary. (Dog is an Animal, but not Cat. We won't do this Cat c = new Animal().)

I hope this explanation is better.

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Hey Himai ;) , rocking explanation , I finally was able to get it. It was better example the last one (at least for me) I am going to quote you and highlight what were the keys to understand it (I hope this way helps others in the same situation , when having burned brain ;) )

Himai Minh wrote:


GenericDemo genDemo = new GenericDemo(); //a non-generic type
GenericDemo result = genDemo.getE();



In this example, GenicDemo genDemo is not bound to any generic type. The "E" is GenericDemo type and "genDemo.getE()" returns GenericDemo type.

In this :

What is E? GenericDemo, the non-generic type. Of course, it can be any subtype of GenericDemo.
What is the return type of e.getE()? GenericDemo.
What is the return type of doStuff? E.
Is GenericDemo the same type of E? Not necessary.
For example, is Animal the same type of Cat? Not necessary. (Dog is an Animal, but not Cat. We won't do this Cat c = new Animal().)

I hope this explanation is better.



Thank you so much Himai.

PS: Sorry for the weird way of highlighting the 2 code lines, b tag won't work inside quote tag.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic type doubt