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