This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Upper Bounded Wildcards/Lower Bounded Wildcards 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 "Upper Bounded Wildcards/Lower Bounded Wildcards" Watch "Upper Bounded Wildcards/Lower Bounded Wildcards" New topic
Author

Upper Bounded Wildcards/Lower Bounded Wildcards

Dante Sparda
Greenhorn

Joined: Jan 12, 2013
Posts: 19

Hi friends,

Please pay attention the code below:



Why the actions in Group1 face with compile error while the Group2 works fine ?

I don't know the answer but it seems that only the following actions are possible:

- Remove
- Clear
- Getting the iterator and invoke remove.
- Capturing the wildcard and write elements that you've read from the list.

The following link can be very helpful but I've not found an answer for the question above yet.
Guidelines for Wildcard Use

I appreciate your help


OCPJP 6 (80%)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi Dante,

Welcome to CodeRanch!

Currently, you have two lists:
1) myList1 - this contains objects of A or its subtype.
2) myList2 - this contains objects of A or its supertype.

Now, the problem is - A can have many subtypes (e.g. X extends A, Y extends A and so on), and hence, myList1 can be a terrible mess of all subtypes of A. To avoid this, Java does not simply allow to add any element to this list.

However, in myList2, all the objects are going to be objects of A (or its supertype) - and hence, myList2 will not be a mess of different objects (that is - objects would be from same hierarchy), and hence, Java allows adding elements in myList2.

So, what's the use of myList1?
Well, you can have a method which accepts a List<? extends A>, and further, while invoking that method, you can pass List<A>, List<B> or List<C> (provided you don't add any element to that list inside the method).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Dante Sparda
Greenhorn

Joined: Jan 12, 2013
Posts: 19

Hi Anayonkar,

Thanks for your answer, It is so helpful
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 762
One more note for discussion:
Dante Sparda
Greenhorn

Joined: Jan 12, 2013
Posts: 19

Himai Minh wrote:One more note for discussion:


I couldn't find any reasonable answer for this
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 762
What I meant was:
Dante Sparda
Greenhorn

Joined: Jan 12, 2013
Posts: 19

Himai Minh wrote:What I meant was:


wow, I got it

It is helpful for the exam

thanks for your help ;)
 
GeeCON Prague 2014
 
subject: Upper Bounded Wildcards/Lower Bounded Wildcards