• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Problem

 
raju ansari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
raju ansari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic