• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic doubt

 
Rex Young
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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 ]
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic