• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Doubt

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Bird
{
void talk() { System.out.print("chirp ");

}


}

class Parrot2 extends Bird
{
protected void talk() { System.out.print("hello ");
}
public static void main(String [] args)
{
Bird [] birds = {new Bird(), new Parrot2()
};
for( Bird b : birds)
b.talk();
}
}

Can anyone explain why teh output is getting as
Chrk,hello
Explain please
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sravanthi pulukuri:
class Bird
{
void talk() { System.out.print("chirp ");

}


}

class Parrot2 extends Bird
{
protected void talk() { System.out.print("hello ");
}
public static void main(String [] args)
{
Bird [] birds = {new Bird(), new Parrot2()
};
for( Bird b : birds)
b.talk();
}
}

Can anyone explain why teh output is getting as
Chrk,hello
Explain please



Hi,
In one word, you see the Polymorphism there.
Parrot has overridden the Bird method talk() with more public access that is
in the Bird class it has default access whereas in the subclass Parrot
it it protected (hence more public).

You have created a Bird array. Its elements hold reference of Bird as
well object of the Bird's subclasses object. Method selection will be done at run time on behalf of what object the ref variable is holding. First
element holds object of Bird so Bird's talk() method is selected and in the
second iteration because the object is now Parrot, talk() method of the
Parrot is selected.




Thanks,
[ May 15, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

class Bird
{
void talk() { System.out.print("chirp ");

}


}

class Parrot2 extends Bird
{
protected void talk() { System.out.print("hello ");
}
public static void main(String [] args)
{
Bird [] birds = {new Bird(), new Parrot2()
};
for( Bird b : birds)
b.talk();
}
}

Can anyone explain why teh output is getting as
Chrk,hello



You are overriding the method talk(). When the method is called it is called based on the object type. So, the first call will invoke the method in the bird class so it prints chirp and during the second call the object is of type parrot2 so the talk() method in the parrot2 class will be invoked and thus prints hello.

Case 1
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops i came to know what mistake was done
thanks
reply
    Bookmark Topic Watch Topic
  • New Topic