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

A question about runtime polymorphism.

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code:


And here is the output:

A
A
A



I wonder why it doesn't print B at second and third lines?
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because class A's printthem() method doesnot know anything about class B's printit() method as A's printit() method is private.
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Sridhar said, printit method in A is private and private methods are not overridden so the call to printit is not polymorphic as printit in B doesn't override printit of A...
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So just to remind myself:
1) Private methods are not overridden / polymorphic
2) Static methods are not overridden / polymorphic

Jim ... ...
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Class B's printit() method is NOT overriding Class A's printit() method because of the fact that Class A's method is Private.

2) Polymorphism applies only to overriden methods and not overloaded or redefined methods.

3) Had it been the case that Class A defines printit() method to be public , then the output at second line would definitely be B, because at that time Class B overrides prinit() method and polymorphism applies to it.


Thanks !!!

 
Get out of my mind! Look! A tiny ad!
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic