Reputation: 41
My goal is to print out that all the instruments are playing by creating the testInstrument.java file. For some reason I am getting an error for System.out.println(all[i].play());
testInstrument.java
package my_instruments;
public class testInstrument {
public static void main(String[] args) {
// TODO Auto-generated method stub
Guitar g = new Guitar();
Flute f = new Flute();
Piano p = new Piano();
Instrument[] all = new Instrument[3];
all[0] = g;
all[1] = f;
all[2] = p;
for (int i=0; i<3; i++) {
System.out.println(all[i].play());
}
}
}
Instrument.java
package my_instruments;
public class Instrument {
public Instrument() {
}
public void play() {
System.out.println("Playing instrument");
}
}
Piano.java
package my_instruments;
public class Piano extends Instrument{
public Piano() {
super();
}
public void play() {
System.out.println("Playing piano");
}
}
Upvotes: 0
Views: 128
Reputation: 6023
your play method() is doing the printing already System.out.println();
try removing the print statement from your for loop
for (int i=0; i<3; i++) {
all[i].play();
}
or
for (Instrument i : all) {
i.play();
}
Upvotes: 1
Reputation: 3429
Try this:
for (int i=0; i<3; i++) {
all[i].play();
}
The play method is already doing the printing and is not returning anything to print.
Upvotes: 2