• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Please explain this code

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii can anyone please expalin me this set of code right from the 1st line till last..what's happening..i'll appreciate your help.

1.class Animal{
2. void makeNoise(){System.out.println("generic noise");}
3.}
4.class Dog extends Animal{
5.void makeNoise(){System.out.println("bark");}
6.void playDead(){System.out.println(" roll over");}
7.}
8.class CastTest2{
9. public static void main(String [] args){
10.Animal [] a={new Animal(),new Dog(),new Animal() };
11.for(Animal animal : a){
12. animal.makeNoise();
13. if(animal instanceof Dog){
14. animal.playDead();
15. }
16. }
17. }
18.}


output;
cannot find symbol
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anamika henry:
hii can anyone please expalin me this set of code right from the 1st line till last..what's happening...


Maybe you could help us (and yourself) by explaining as much of it as you can, and then telling us where you're stuck.
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain me from line 10-14.
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain me from line 10-14.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will print

generic noise bark roll over generic noise

first of all the first element of array is a animal object so
generic noise will be printed as Animal version of the makeNoise method is called.

second object in the array is a dog object so Dog version of makeNoise will be called,then as the second array element is instance of dog roll over will be printed

third is same as first.
[ April 13, 2007: Message edited by: sharan vasandani ]
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
henry


10.Animal [] a={new Animal(),new Dog(),new Animal() };
11.for(Animal animal : a){
12. animal.makeNoise();
13. if(animal instanceof Dog){
14. animal.playDead();
15. }



first it will complie time error at line 14

because at compile time ,the compiler checks at line 13 wheather is there any relation between Animal and Dog ,if it founds any relation it won't give any error ,here in your case the relation is IS-A means DOA IS-A ANIMAL
ok

And then at line 14 the compiler checks with anilmal referenc wheather playDead() is there that method in Animal class ,but in your case that mehod is not there,that is why it is giving compile time error,put that method in Animal class you won't get any error.



Here the first reference is refering to Animal
2nd Dog
3rd Animal
The method which has to be executed is depends on Object which is refering by the reference

I think you got it.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will not work!
The first class define an Animal
The second class define a Dog
The main loop over an array of Animal BUT on line 14

will give you an error because Animal does not have any playDead() method.

at this point you can correct the code in two way:
1) add a playDead() to the Animal class
2) modify the line 14 to

you can do it without problem because before you tested
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anamika ,

Just see the below code, Hopefully it helps

package com.org.zoo;


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");}
}
public class Funny {
public static void main(String[] args) {
// Animal array will accept all things that are animals
Animal [] a={
new Animal(),
new Dog(), // As Dog is a animal it is added into Animal array
new Animal()
};
for(Animal animal : a){
// Pick each animal from the Animal array
// Call the overridden methord makeNoise
animal.makeNoise();
// As this methord is overridden for Dog it will call dogs bark
if(animal instanceof Dog)
{
//animal.playDead();
// Animal CLASS DOES NOT HAVE PLAYDEAD METHORD
// DOWN CASTING TO DOG CLASS
//As we can see only what is avaliable in Animal through a AnimalReference

System.out.println("The down casted dog is rolling over..excuse me..");

((Dog)animal).playDead();

}
}}}
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops sorry,

yes you wil have to cast the second element of array to dog before calling playDead method on it
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for your explainations..it was of great help
reply
    Bookmark Topic Watch Topic
  • New Topic