• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic