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 Generic doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic doubt" Watch "Generic doubt" New topic
Author

Generic doubt

Rex Young
Greenhorn

Joined: Jul 21, 2008
Posts: 3
Hi,Please have a look at he following code:

import java.util.*;


public class Basket<E>

{
List<Red> red;
List<Apple> applw;

private E element;

public void setElement(E x)
{
element=x;
}

public E getElement()
{
return element;
}

public <A extends Apple> List<? super Apple> insertRipe(A a,Basket<? super Apple> basket)
{

basket.setElement(new Red());

return red;//Line 1
}

public static void main(String[] args)
{}

}

class Fruit{}

class Apple extends Fruit{}

class Red extends Apple{}

**********************************

Th code returns an error on 'Line 1'.The return type of the method is '? super Apple' so according to me it can return 'red'.

Please let me know where is the issue,

Thank you.
Rex Young
Greenhorn

Joined: Jul 21, 2008
Posts: 3
Also,in the above code:
The line basket.setElement(new Red()) inserts a Red object where a '? super Apple' is expected....Why does this work when 'Line 1' doesn't?

Thanks again..
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi Rex,

Its been around 4 months, when I studied generics, truly saying it is out of my mind now, because i have not used them till now..still when i was trying to solve your problem, one line dint hit my head, can you help me to understand the following:

public <A extends Apple> List<? super Apple> insertRipe(..)

What is the meaning <A extends Apple> ?
As per the method Return type is : List<? super Apple>


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
@Sunny
That is how generic method is written, when you want just a generic method not a generic class.. If it was a generic method you would write it as

class SomeClass<A extends Apple> {
public List<? super Apple> insertRipe(..)
}
<A extends Apple> this is just to put restrictions on TYPE A


@Rex
The return type of the method is List<? super Apple> . The reference variable red is declared to be List<Red> red; Well you can answer this simple question.... is (class)Red (which represents ?) super to (class)Apple?? answer is no! Because class Red extends Apple{}
Check out if this works...return ( <Apple> ) red;

By the way, this sounds absurd to me...
public <A extends Apple> List<? super Apple> insertRipe(A a,Basket<? super Apple> basket)
I think it should be List

Hope it Helps!
[ July 21, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
Working on SCWCD/OCPJWCD
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello,

1st, try to use the tag CODE for codes next time, make it a lot easier to understand.
So, let's go to your code and show you the points you asked:



The return type is List<? super Apple>. It means you can have as return these lists: List<Apple>, List<Fruit> and List<Object> and you can't return a List<Red>.

For basket.setElement(new Red()); it's ok because basket has the type Basket<? super Apple>, it means the list will be List<Apple>, List<Fruit> and List<Object> and these lists accept a Red.

Well, that's all. If you still have any doubt, feel free to ask and I'll try to make it more clear.

Kind Regards,
Raphael Rabadan
[ July 22, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic doubt
 
Similar Threads
Confused by mixed generic type
Doubt About Generic
Generics doubt.
Generics Doubt
method types in Generics