Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Generics !

 
Stein Vom
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stein Vom
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same doubt... can somebody explain that ??? please



[ July 29, 2008: Message edited by: Lino Larios ]
 
Stein Vom
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic