File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 18745
    
  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.
 
jQuery in Action, 2nd edition
 
subject: Generic Problem