• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
Anurag Blore
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Answer: CDG

why B is incorrect//As Ber extends Animal
why E is incorrect as anything which extends Animal should be able to assigned to an Animal.
 
Deepaks Deshpande
Ranch Hand
Posts: 114
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anurag Blore:


Answer: CDG

why B is incorrect//As Ber extends Animal
why E is incorrect as anything which extends Animal should be able to assigned to an Animal.

Hi Anurag,

"list<Bear>" is assignable to only a "list<Bear>" and nothing else not its subtype, not its supertype, so it cannot be assigned to list<Animal>.

And about "list<? extends Animal>" is assignable to "list<Animal>"- i think you are right!
"list<? extends Animal>" means anything which is a subtype of animal, including animal can be assigned(Bear,Cow,D,Animal).
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel multiple answers are correct here.
A. The type List<Animal> is assignable to List.
List rawList = null;
List<Animal> animalList = null;
rawList = animalList;
This works fine because you are assigning a list that contains only Animal objects to a raw list that can contain anything.
This is legal , because JDK 1.5 supports assiging a generic list to a raw list.
A- Correct
B. The type List<Bear> is assignable to List<Animal>.
This is wrong, a list of type List<Bear> can be assigned only to lists of type List<Bear> or List<?> [Assign me anything am happy] or List<? extends Bear> [Assing me a list that can contain Bear or anything that extends Bear]
B- InCorrect

C. The type List<Object> is assignable to List<?>.
Since List<?> will accept any type of list this is legal.
C- Correct

D. The type List<D> is assignable to List<? extends Bear>.
List<? extends Bear>: A list that can contain Bear or anything that extends Bear. Since D extends Bear List<D> can be assigned to a reference of type List<? extends Bear>
D- Correct


E. The type List<? extends Animal> is assignable to List<Animal>.
List<Animal>: Can be assinged List<Animal> only. Now List<? extends Animal> means that this list can contain objects of type Animal or anything that extends Animal which can be Dog, Cat and so on. And hence this list cannot be assigned to a List of type List<Animal>.
E- InCorrect

F. The type List<Object> is assignable to any List reference.
If any list reference refers to a raw list then this statement is correct but otherwise its not correct and hence this answer is wrong.
F- InCorrect

G. The type List<? extends Bear> is assignable to List<? extends Animal>.
List<? extends Animal> = List<? extends Bear> ???
Here <? extends Animal> means that this list can contain objects of type Animal or anything that extends Animal which can be Bear , Cow or D.
[Bear, Cow, D]
Now List<? extends Bear> means that this list can contain Bear or D since D extends D.
[Bear, D]
Since [Bear, D] is a subset of [Bear, Cow, D], This legal.
G- Correct

Hence correct answer is
A- Correct
C- Correct
D- Correct
G- Correct
[ January 11, 2008: Message edited by: Deepak Jain ]
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,
A,C and D are agreed , but I am still not getting how G is correct.
lets take the following code
import java.util.*;

class Animal {}
class Bear extends Animal {}
class Cow extends Animal{}
class D extends Bear {}
public class UseAnimal
{

public static void main(String []s)
{
List <? extends Animal>a=new ArrayList<? extends Bear>();
}
}

It gives a compiler error.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag, when you copy a question from a book or mock exam you need to quote your sources.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am Sorry. That was a mistake answer G is incorrect.
G) The type List<? extends Bear> is assignable to List<? extends Animal>.
List<? extends Animal> can contain [Bear, Cow, D]
List<? extends Bear> can contain [Bear , D].
Since [Bear , D] does not match [Bear, Cow, D] this is incorrect.

Since List<? extends Animal> can contain [Bear, Cow, D], Suppose this list contains Cow's then obiviously this cannot be assigned to List<? extends Bear> because Cow does not extend Bear.

So the correct answer to the question is

A- Correct
C- Correct
D- Correct
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak,
Lets say let mee change my code to

class Animal2 {}
class Bear extends Animal {}
class D extends Bear {}
public class UseAnimal
{

public static void main(String []s)
{
List <? extends Animal>a=new ArrayList<? extends Bear>();
}
}

Still I get a compiler error though [Animal Bear D ] matches with [Bear D]
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

the answer G is correct. The problem of the code is that you cannot use ? with the object instantiation. Therefore
List <? extends Animal>a=new ArrayList<? extends Bear>();
is not allowed ( The instantiation new ArrayList<? extends Bear>() is not possible). But you can prove G with following code

List <? extends Animal> a;
List <? extends Bear> b;

b = new ArrayList<D>(); // D extends Bear
a = b; // assignment is possible because the wildcard ? extends Animal includes the wildcard ? extends Bear

Hope i explain understandable

following code compiles without error:

 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serg, you are absolutely right.
 
Anurag Blore
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Thanks for replying to this post, I was not well so cpuld't check your answers.

I got this question from one of the link from this page
http://cafe4java.com/mockexams/mockexamlist.php

I amnot sure which link as I copied all the Generics related questions and started working.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic