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]
# 1
Did you simply try to substitute Object with Animal?
Jocoa at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 2
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
mohammedsajida at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 3
Looks ok to me, and indeed compiles just fine
georgemca at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 4

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

ramkria at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 5
> plz helpNo idea! It does compile, it executes fine. What version of JDK are you using?
aniseeda at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 6
Do you have a class of your own entitled 'Object'?If so, don't.
ejpa at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 7
> Do you have a class of your own entitled 'Object'?Good eye!
jverda at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...
# 8
jdk 1.5
ramkria at 2007-7-12 22:19:49 > top of Java-index,Java Essentials,Java Programming...