Does class A have a b variable? a3 is defined as an instance of an A object.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
I guess that the toString() method is overriden.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
But why does printing out a3 uses toString() method from class B?
it doesn't. but, you stated:
a3 = b3;
from that moment on, a3 actually is an instance of B, that's why it'll look for the toString method in the B class first, only if it doesn't find one there, it'll go to the parent class.
stultuske
Industrious Poster
4,377 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
Yes,it is as if doing an implicit casting of a child type object to a parent type reference.So a3 now refers to the child type object and during runtime,due to dynamic method despatching,it uses toString of B.
delta_frost
Junior Poster in Training
69 posts since Aug 2009
Reputation Points: 10
Solved Threads: 9
Skill Endorsements: 2