my dog learned polymorphism*
The moose likes Java in General and the fly likes about arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "about arrays" Watch "about arrays" New topic
Author

about arrays

Kamani Ellegama
Greenhorn

Joined: Jun 14, 2010
Posts: 8
class Animal {
void makeNoise() {System.out.println("generic noise"); }
}
class Dog extends Animal {
void makeNoise() {System.out.println("bark"); }
void playDead() { System.out.println("roll over"); }
}
class CastTest2 {
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal() };
for(Animal animal : a) {
animal.makeNoise();
Dog d=(Dog) animal{
if(animal instanceof Dog) {
animal.playDead();
}
}
}
}
}

im in confusing thing that done in the for loop?(for(Animal animal:a) ) can you please describe it.
deepak kushwaha
Ranch Hand

Joined: Jul 17, 2010
Posts: 33
this is the advanced format of run. It says for all object in the 'array a' will we assigned to Animal variable one by one. In other words first the new animal() object will be assigned to a and the loop will run, then new dog() will be assigned to 'a' and the loop will run again and so on for all the elements in array. and since dog is the subtype of animal this assignment is allowed.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch . use code tag while posting your code, so that it can be easy to read.

read this : http://www.javabeat.net/articles/32-new-features-in-java-50-1.html

by the way : code in your for loop is syntactically incorrect . is it compiled?
Kamani Ellegama
Greenhorn

Joined: Jun 14, 2010
Posts: 8
thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Kamani Ellegama wrote:thanks

you are welcome
Kamani Ellegama
Greenhorn

Joined: Jun 14, 2010
Posts: 8
Seetharaman Venkatasamy wrote:Welcome to JavaRanch . use code tag while posting your code, so that it can be easy to read.

read this : http://www.javabeat.net/articles/32-new-features-in-java-50-1.html

by the way : code in your for loop is syntactically incorrect . is it compiled?


it should be change to
class CastTest2 {
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal() };
for(Animal animal : a) {
animal.makeNoise();
Dog d=(Dog) animal;
if(animal instanceof Dog) {
d.playDead();
}
}
}
}
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

instanceof operator check should be done before you cast Animal object to Dog.
Kamani Ellegama
Greenhorn

Joined: Jun 14, 2010
Posts: 8

thanks again for helping.
above code compile perfectly but run time exception was thrown" Exception in thread ''main" java.long.NoclassDefFoundError: ClassTest2" when it run. can you please tel me the reason??
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

The name of the class is "CastTest2" - not "ClassTest2"


Regards, Rene Larsen
Dropbox Invite
Kamani Ellegama
Greenhorn

Joined: Jun 14, 2010
Posts: 8
Rene Larsen wrote:The name of the class is "CastTest2" - not "ClassTest2"

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about arrays
 
Similar Threads
Reference Variable Casting
Do I make it wrong?
Confused with instanceof, please help
Please explain this code
Reference variable casting