aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Question 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 "Generics Question" Watch "Generics Question" New topic
Author

Generics Question

Anurag Blore
Ranch Hand

Joined: Jan 15, 2003
Posts: 74


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.


Thanks,
Anurag
SCJP 1.2 & 1.5, PMP
Deepaks Deshpande
Ranch Hand

Joined: Dec 06, 2007
Posts: 111

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).


Regards,
Deepak

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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 ]

SCJP, SCWCD, SCBCD
Charmy Madhvani
Ranch Hand

Joined: Dec 26, 2007
Posts: 59
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.


SCJP 5.0<br />SCWCD 5.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Anurag, when you copy a question from a book or mock exam you need to quote your sources.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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

Joined: Dec 26, 2007
Posts: 59
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

Joined: Dec 17, 2007
Posts: 49
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:



SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Serg, you are absolutely right.
Anurag Blore
Ranch Hand

Joined: Jan 15, 2003
Posts: 74
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Question
 
Similar Threads
generics question
Generics doubt
SCJP 6 question
Generics
Generics Question: