This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Problem " Watch "Generic Problem " New topic
Author

Generic Problem

raju ansari
Greenhorn

Joined: Oct 28, 2008
Posts: 7
Hi all,
Please advise on the below query
Code
-----------------------------------------------------------
interface Instrument {
public void play();
}
class Guitar implements Instrument {
public void play(){
System.out.println("Playing Guitar.");
}
public String toString()
{
return "I am Guitar";
}
}
class MyGuitar extends Guitar
{
public String toString()
{
return "I am MyGuitar";
}
}
class yourGuitar extends MyGuitar
{
public String toString()
{
return "I am yourGuitar";
}
}
-----------------------------------------------------------------
Now I declare following lines in main method.
List<? super yourGuitar> allInstruments = new ArrayList<Instrument>();
allInstruments.add(new Guitar());
allInstruments.add(new yourGuitar());
--------------------------------
It is giving me error :
C:\javaProject\src>javac DemoGen.java
DemoGen.java:33: cannot find symbol
symbol : method add(Guitar)
location: interface java.util.List<capture of ? super yourGuitar>
allInstruments.add(new Guitar());
^
1 error
-----
I thought we can add any object who are above in the hierarchy of yourGuitar.

Thanks in advance


Regards,<br />Raju Ansari
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

I thought we can add any object who are above in the hierarchy of yourGuitar.


Actually, no... when you declare this...

Now I declare following lines in main method.
List<? super yourGuitar> allInstruments = new ArrayList<Instrument>();


allInstruments is a list that takes something that IS-A super class of yourGuitar. It is *not* a list that takes anything that is a super class of yourGuitar.

All Java knows is, this list takes something that IS-A super class of yourGuitar, and it doesn't know what it is. So... in order for you to add to this list, you must add something that IS-A everything that is a super class of your guitar, which will guarantee that it IS-A whatever it is supposed to be.

And the only objects that IS-A super class of your guitar, are yourGuitar objects, which includes sub-classes of yourGuitar. So, you can only add yourGuitar, and subclasses of yourGuitar objects.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
raju ansari
Greenhorn

Joined: Oct 28, 2008
Posts: 7
Thanks a lot for such a quick reply...

It seems that we can add only subclass of yourGuitar not the objects who are above in the hierarchy of yourGuitar class.

Could you please explain a bit about "<? super yourGuitar>"?

K&B says using "<? super yourGuitar>" in method argument give you liberty to add objects who are above in the hierarchy of yourGuitar class. But here we can only add the objects who are subclass of yourGuitar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Problem