File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about arrays

 
Kamani Ellegama
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamani Ellegama wrote:thanks

you are welcome
 
Kamani Ellegama
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instanceof operator check should be done before you cast Animal object to Dog.
 
Kamani Ellegama
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of the class is "CastTest2" - not "ClassTest2"
 
Kamani Ellegama
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene Larsen wrote:The name of the class is "CastTest2" - not "ClassTest2"

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic