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(); }
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 [] ..).
0
You didn't initialize pet[]