• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

about arrays

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamani Ellegama wrote:thanks


you are welcome
 
Kamani Ellegama
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instanceof operator check should be done before you cast Animal object to Dog.
 
Kamani Ellegama
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name of the class is "CastTest2" - not "ClassTest2"
 
Kamani Ellegama
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rene Larsen wrote:The name of the class is "CastTest2" - not "ClassTest2"


 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic