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 Enthuware: Doubt on 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 "Enthuware: Doubt on Generics Question" Watch "Enthuware: Doubt on Generics Question" New topic
Author

Enthuware: Doubt on Generics Question

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
This is a Drag and Drop Question from Enthuware (com.enthuware.ets.scjp.v6.1.824)



choices:

(a) dataList.add(t);
(b) dataList.add(b);
(c) t = dataList.get(0);
(d) b = dataList.get(0);


I would have answered (b) for line //1 because dataList should be a List that contains either a Dooby or a superclass of Dooby. Therefore, only objects of Booby or Dooby can be added to the list. So I answered (b).

But the answer is,

//1 = (a)
//2 = (d)

With this explanation.

1. addData1(List<? super Dooby> dataList) :
This means that dataList is a List whose elements are of a class that is either Dooby or a super class of Dooby. We don't know which super class of Dooby. Thus, if you try to add any object to dataList, it has to be a assignable to Dooby.
Thus, dataList.add(b); will be invalid because b is not assignable to Dooby.
Further, if you try to take some object out of dataList, that object will be of a class that is either Dooby or a Superclass of Dooby. Only way you can declare a variable that can be assigned the object retrived from dataList is Object obj. Thus, t = dataList.get(0); and b = dataList.get(0); are both invalid.


2. addData2(List<? extends Dooby> dataList)
This means that dataList is a List whose elements are of a class that is either Dooby or a subclass of Dooby. Since we don't know which subclass of Dooby is the list composed of, there is no way you can add any object to this list.

If you try to take some object out of dataList, that object will be of a class that is either Dooby or a subclass of Dooby and thus it can be assigned to a variable of class Dooby or its superclass.. Thus, t = dataList.get(0) ; is invalid.



It says in #1 that "we don't know which super class of Dooby", wherein the only superclass of Dooby in the program that we know is Booby...

This really started to confuse me. Can somebody please support my answer? Or do you agree with the answers?
[ September 21, 2008: Message edited by: Denise Saulon ]

SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCMJEA in progress...
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Hello Denise,

I would have answered (b) for line //1 because dataList should be a List that contains either a Dooby or a superclass of Dooby. Therefore, only objects of Booby or Dooby can be added to the list. So I answered (b).


No, this ist not right. The boundaries in the method signature ensure that only Lists of a supertype (including Dooby) are passed as method arguments. However, you can definitely add instances of extending classes to such a list.
Suppose, the provided List is of kind Dooby. Then, you cannot add an instance of Booby to this list.

Hope it helps,
Thomas


SCJP 5.0, SCJD in progress
Muhammad Ahsan Jamshaid
Ranch Hand

Joined: Jun 01, 2006
Posts: 59
While using wild card in 1.5 you can only access the collection and you can't modify. Is it chaged in 1.6 or my concept is wrong?

--Jamshaid..


--Ahsan Jamshaid... SCJP 5(80%)
Jarek Jankowski
Greenhorn

Joined: Jul 05, 2008
Posts: 13
That is a nice question in the topic of generics. Thanks Thomas for your clarification.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Help me if I am wrong but, the way I look at <? super ClassName>
in this context, is that I am telling the compiler to only allow a list of ClassName or a superclass of Classname to be passed. With that the highest class object that can be added to the list is ClassName. You can't add anything higher because the compiler can't guarantee it and the runtime can't do anything at all about it. Whatever gets in will be superclass of ClassName so you may add a ClassName or a subclass of ClassName.


------------------------
Bob
SCJP - 86% - June 11, 2009
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Yes Bob, you are right...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
 
subject: Enthuware: Doubt on Generics Question
 
Similar Threads
map is an ArrayList, compiler knows that Object is superclass of ArrayList, yet does nothing?
API Contents - I/O
Collections and Generics
What is wrong with my generic implementation of sort method ?
Generics Question: