Hi all!
I saw this program in some website. It says '? extends Instrument'. Doesn't that means that all the subclasses of Instrument and Instrument itself can be added here? Correct me if i'm wrong. Because it is showing compilation error. The compilation error was gone, when i changed the extends to super. But 'super' means that only the super classes can be added. I'm just confused.
public class Ques {
public static void main(String[] args) {
List<? extends Instrument> allInstruments = new ArrayList<Instrument>();
allInstruments.add(new Guitar());
allInstruments.add(new Violin());
}
}
interface Instrument {
public void play();
}
class Guitar implements Instrument {
public void play(){
System.out.println("Playing Guitar.");
}
}
class Violin implements Instrument {
public void play(){
System.out.println("Playing Violin.");
}
}
// end of program...