0

i dont get the code on the end what should make the pet giving both Meow and Woof especially for (int i=0; i<2; i++) {

class Animal { public void makeSound() { System.out.println("Grr..."); } } class Cat extends Animal { public void makeSound() { System.out.println("Meow"); } } class Dog extends Animal { public void makeSound() { System.out.println("Woof"); } } class Program { public static void main(String args[ ]) { Animal a = new Dog(); Animal b = new Cat(); a.makeSound(); b.makeSound(); } } Public Static Void Main(String[] args){ pet[0] = new Dog(); pet[1] = new Cat(); for (int i=0; i<2; i++) { pet[i].makeSound(); }

14th Jan 2019, 9:59 PM
Nuh Cetin
Nuh Cetin - avatar
2 Respostas
+ 1
Animal[] pet = new Animal[2]; pet[0] = new Dog(); pet[1] = new Cat(); Then your loop will work. (like String [] string = new String [] or int [] number = new number [] ..).
15th Jan 2019, 6:37 AM
Denise Roßberg
Denise Roßberg - avatar
0
You didn't initialize pet[]
15th Jan 2019, 3:16 AM
Luis Méndez
Luis Méndez - avatar