This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Generics ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Generics !" Watch "Regarding Generics !" New topic
Author

Regarding Generics !

Stein Vom
Greenhorn

Joined: Jun 07, 2002
Posts: 5
Hi all!
I saw this program in some website. It says '? extends Instrument'. Doesn't that means that all the subclasses of Instrument and Instrument itself can be added here? Correct me if i'm wrong. Because it is showing compilation error. The compilation error was gone, when i changed the extends to super. But 'super' means that only the super classes can be added. I'm just confused.

public class Ques {
public static void main(String[] args) {
List<? extends Instrument> allInstruments = new ArrayList<Instrument>();
allInstruments.add(new Guitar());
allInstruments.add(new Violin());
}
}
interface Instrument {
public void play();
}
class Guitar implements Instrument {
public void play(){
System.out.println("Playing Guitar.");
}
}
class Violin implements Instrument {
public void play(){
System.out.println("Playing Violin.");
}
}
// end of program...


Steinvonmars
Stein Vom
Greenhorn

Joined: Jun 07, 2002
Posts: 5
I have seen other people discussing on similar topic. They were saying that null can be added. Ok, null can be added. But I want to know why we are not able to use something, what it is meant for?
Lino Larios
Greenhorn

Joined: Jun 27, 2008
Posts: 28
I have the same doubt... can somebody explain that ??? please



[ July 29, 2008: Message edited by: Lino Larios ]
Stein Vom
Greenhorn

Joined: Jun 07, 2002
Posts: 5
Hi Lino! Thanks for the reply.
I thought i'm the only one who is still in doubt about this topic.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
please post code samples using the code tags.

If you use:

then you can add any subtype of instrument to the allInstruments list. Any time you attempt to retrieve anything from the List the reference type will always be Instrument. Also the generic type of the ArrayList on the right hand side must match the type declared on the left. So an ArrayList of Guitar would not be valid here.

However if you use the wildcard syntax on the left hand side (? extends Instrument) then the generic type on the right hand side can be any sub type of Instrument. But the flipside of this is that the compiler does not know exactly what type the list is. For example:


In later uses of the allInstruments reference variable, the compiler only knows that the list will contain a subtype of Instrument so if it were to allow an add then it would have to allow for addition of Violin/Guitar etc but as can be seen from my example, the actual list is of Guitar. So the compiler plays safe and only allows you to add null.

Hope this helps. See the java generics tutorial for more info.
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
This is explained nicely in K&B Book.
Here is the extract for you.



Now as per book above line means :



By saying <? extends Instrument>, we're saying, "I can be assigned a collection that is a subtype of List and typed for <Instrument> or anything that extends Instrument. And oh yes, I SWEAR that I will not ADD anything into the collection."

Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Saurabh Vyas:
This is explained nicely in K&B Book.
Here is the extract for you.



Now as per book above line means :



And just an addition for what Saurabh Vyas said:
The only way wou can add something to a Collection declared with a wildcard is using <? super SomeClass>.
You'll only be able to add the type SomeClass to this Collection, let me try to explain why:
Using an Collection<? super SomeClass> you assure that will be passed any Collection that can contains a SomeClass, I'll use an String as example.
String has CharSequence and Object as parent.
So, if you have an List<? super String> means you can have a list of String, CharSequence or Object, and all of these lists can have a String added to it.
See this code:


I hope it helps.

Kind Regards,
Raphael Rabadan
[ July 30, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Generics !
 
Similar Threads
list interface
mock exam question
Generic Problem
Generics Prob
Generics for Interface