0% found this document useful (0 votes)
3 views1 page

Legal Generics Usage in Java

Uploaded by

rbkothari4776
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Legal Generics Usage in Java

Uploaded by

rbkothari4776
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Generics Question

1. Given
public class Fruit{}
public class Melon extends Fruit{}
public class WaterMelon extends Melon{}

Which of the following, will be legal ?

[Link]<? extends Fruit> fruits=new ArrayList<Fruit>();


[Link]<? extends Fruit> fruits=new ArrayList<Melon>();
[Link]<? extends Fruit> fruits=new LinkedList<WaterMelon>();
[Link]<? extends Fruit> fruits=new Vector<Object>();
[Link]<? super Melon> melons=new ArrayList<Fruit>();
[Link]<? super Melon> melons=new LinkedList<>();
[Link]<? super Melon> melons=new LinkedList<WaterMelon>();

2.
Given
public class Fruit{}
public class Melon extends Fruit{}
public class WaterMelon extends Melon{}

Which of the following, will be legal ?


Help : API of List /Set i/f : boolean addAll(Collection<? extends E> c)

1. List<Melon> melons=new ArrayList<>();


2. HashSet<Fruit> fruits=new HashSet<>();
3. LinkedList<WaterMelon> wMelons=new LinkedList<>();
4. [Link](fruits);
5. [Link](wMelons);
6. [Link](melons);
7. [Link](wMelons);
8. [Link](fruits);
9. [Link](melons);

You might also like