advanced for loop: incompatible & inconvertible types
class Animalimplements java.util.Iterator{}
class Dogextends Animal{}
class Catextends Animal{}
class Vet{
publicstaticvoid main(String [] args){
Animal [] aa ={new Dog(),new Dog(),new Dog()};
for(Object o : aa)// compiler error.
// Incompatible type. found: Animal required: Object
goWalk((Dog) o);// inconvertible type. found: Object required: Dog
}
staticvoid goWalk(Dog d){}
}
i'm getting 2 compile time error. could someone explain why?
thanks
[1543 byte] By [
ramkria] at [2007-11-27 9:24:08]

HiThere is no error in the given code....U can Used both "Object" or "Animal", doen't make any difference..Except that, u make sure that u have implement all the methods of the java.util.Iterator Interface to Animal Class, As I can see it empty...BR
Hi Guys
i dont know why i'm getting 2 compile-time errors. Small change in the code: it doesn't implement Iterator interface.
class Animal{ }
class Dog extends Animal{ }
class Cat extends Animal{ }
class Vet {
public static void main(String [] args) {
Animal [] aa = {new Dog(), new Dog(), new Dog()};
for(Object o : aa)
goWalk((Dog) o);
}
static void goWalk(Dog d) { }
}
plz help