I am writing a code that has an ArrayList
of the names of dogs. An interface method allows the animals to make sounds (ex. "woof"
). I have a for loop that goes through the entire array and determines if the animal says. What I am having trouble with is that the obj.speak()
is not working. Whenever I run it, it would say the symbol is not found but I am confused as I already put it there. I am not sure how to counter this issue as I went on serval websites to find answers but they were no help. I assume that the ArrayList
is being looped over as I put a for-loop to go through the entire thing.
public class Main {
public static void main(String[] args) {
ArrayList dogs= new ArrayList();
dogs.add(new Dog("Fred"));
dogs.add(new Dog("Wanda"));
for (Object e: dogs) {
e.speak();
}
}
}
interface Speak{
public void speak();
}
class Dog implements Speak {
private String name;
public Dog(String name) {
this.name = name;
}
public void speak() {
System.out.println("Woof");
}
}