Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Moderator: admin

Post Reply
admin
Site Admin
Posts: 7472
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Post by admin » Wed May 18, 2011 8:30 am

Three basic principles:

1. The main purpose of having generics is to avoid putting random things into any collection. After all, you want things organized. You don't want to put apples in a sack of potatos. And when you are given a sack of apples, you expect apples to come out and not potatos. At the same time, if you are given a "basket of fruit" as a gift, you should be able to put an orange, an apple, a mcintosh apple, or any other fruit into it. Right? And when you pick a fruit from that basket, do you always expect to get an apple? No, right?

That's the fundamental thing and the hardest to absorb (I bet you are rolling your eyes ).

Now, the simpler stuff:

2. ? extends Apple: This means "of a class that extends Apple". Therefore, List<? extends Apple> means a sack of some variety of Apple. Mcintosh? Probably. But you don't know.

Q. When you are given a sack of "? extends Apple", can you put a Mcintosh in it? If you have any doubt, read point 1 again. (Hint: Do you want to mess up your sack of Fuji apples by putting a Mcintosh apple it it?)
Q. When you take out stuff from a sack of "? extends Apple", what do you get? An Apple for sure. (You don't mind if it is a Mcintosh or a Fuji, do you? As long as you don't get a Potato, you are good.)

3. ? super Apple: This means "of a class that is a super class of Apple". Therefore, List<? super Apple> means a sack of something of which an Apple is a kind. Let's call it "it". Now play Jeopardy. So, What do you call a sack of something of which an Apple is a kind? A sack of fruits. Are you sure? Can you not call it a sack of food items? or A sack of Mcintoshes? (Hint: Every apple is a kind of fruit. Every apple is a kind of food item. Is every apple a kind of Mcintosh?) So which of these statements should work?

Code: Select all

listOfsuperApple.add(anApple); 
listOfsuperApple.add(someFruit); 
listOfsuperApple.add(aMac); 
Read 1 again, if in doubt.

Q. When you take out stuff from a sack of "? super Apple", what do you get? Apple? Probably. A fruit? probably. Some food item? probably. Are you sure? No. So what are you sure of? Well, it is something, but you don't know what. So you are sure of nothing, really. So which of these statements should work?

Code: Select all

Apple a = listOfsuperApple.get(0); 
Fruit a = listOfsuperApple.get(0); 
FoodItem a = listOfsuperApple.get(0); 
Object a = listOfsuperApple.get(0); 
Read 1 again, if in doubt.

Q. When you are given a sack of "it/something", can you put any apple in it? Well, can you not put any apple in a sack of fruits? in a sack of food items? In a sack of apples?

Read 1 again, if in doubt.

That's it! If you understand point 1, you will never miss any question on generics. Ever!

Quiz: What would List<?> mean? What can you put in it and what can you take out from it?
If you like our products and services, please help us by posting your review here.

Kostik
Posts: 2
Joined: Sat Oct 27, 2012 3:35 am
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Post by Kostik » Sat Oct 27, 2012 3:48 am

The best explanation of wildcards which I have ever seen!
Thanks

admin
Site Admin
Posts: 7472
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Post by admin » Tue Oct 30, 2012 8:46 am

When to use ? super Apple and when to use ? extends Apple

Very easy - if you want to eat Apples from a bag, you should look for a bag of Apples. i.e use ? extends Apple. For example, List<? extends Apple>. This will allow you to take out Apples from the bag because you know that whatever you take out of this bag will definitely be an Apple. Remember, anything that extends Apple IS-A Apple.

If you want to dump Apples into a bag, you can dump them into a bag of Apples or into a bag of Fruits or into a bag of FoodItems. But you can't dump them into a bag of FujiApples, right? (Because not every Apple is a FujiApple, don't forget that!) In other words, use ? super Apple. For example, List<? super Apple>. This will let you add any Apple to the list.

Quiz
1. What can you dump into List<? extends Apple>?
2. What can you eat from List<? super Apple>? (Hint, can you eat Object?)
If you like our products and services, please help us by posting your review here.

Deepa

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Post by Deepa » Thu Dec 06, 2012 12:05 pm

Do we have this topic Generics for OCAJP 7 exam?

admin
Site Admin
Posts: 7472
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Concepts and Quiz on Generics ( com.enthuware.ets.scjp.v6.1.824 )

Post by admin » Thu Dec 06, 2012 1:03 pm

Deepa wrote:Do we have this topic Generics for OCAJP 7 exam?
No.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 14 guests