• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware: Doubt on Generics Question

 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Jarek Jankowski
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a nice question in the topic of generics. Thanks Thomas for your clarification.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Bob, you are right...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic