• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enthuware: Doubt on Generics Question

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a nice question in the topic of generics. Thanks Thomas for your clarification.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Bob, you are right...
reply
    Bookmark Topic Watch Topic
  • New Topic