Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about runtime polymorphism.

 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • 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?
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • 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 ... ...
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • 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 !!!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic