This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this code" Watch "Please explain this code" New topic
Author

Please explain this code

anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
can anyone explain me from line 10-14.
anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
can anyone explain me from line 10-14.
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
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 ]
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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.
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
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
Raja Manikamam
Greenhorn

Joined: Mar 29, 2007
Posts: 1
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

Joined: Feb 22, 2007
Posts: 100
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

Joined: Mar 09, 2007
Posts: 30
Thank you everyone for your explainations..it was of great help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain this code
 
Similar Threads
Reference variable casting
Reference Variable Casting
Reference Variable Casting?
about arrays
Confused with instanceof, please help