Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected result in constructor call

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading Deitel's Java book and he had an example where a constructor called its parent constructor. Both had a "toString" method [so that both were overriding Object's "toString"]. I was surprised to see that when the parent constructor was executing [as always, prior to the child constructor] it used the child constructor's "toString" instead of its own.

I'm trying to recall what Java rule forced this to occur?

Thanks in advance to all replies,
Dan
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java methods are always polymorphic. Even before an object is fully constructed, method calls will always go to the most derived version of a method.

If you're coming from a C++ background, this can be surprising, as C++ has a complicated set of rules for how the type of an object "evolves" during construction. In Java, the rule is very simple: if you've allocated a Foo object, then it's always a Foo object, even while superclass constructors are running.

Can this get you into trouble? Yes, sometimes. Some people advocating following the rule "Never call a non-private or non-final method from a constructor."
 
Scott Selikoff
author
Saloon Keeper
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the number one issue in situations like this that disturbs me a little is if I cast an object as a specific parent class, and call the parents method, its child's version of a method will be called (if such a version is overriden).

Not that I shouldn't expect it, but it definitely makes reading/debugging code a little bit confusing at times. Keep in mind though, if not for this 'wonderful' behavior of runtime method decisions the singleton factory pattern wouldn't work that well.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
I think the number one issue in situations like this that disturbs me a little is if I cast an object as a specific parent class, and call the parents method, its child's version of a method will be called (if such a version is overriden).

Not that I shouldn't expect it, but it definitely makes reading/debugging code a little bit confusing at times. Keep in mind though, if not for this 'wonderful' behavior of runtime method decisions the singleton factory pattern wouldn't work that well.


Why should it be disturbing???
In Java, the bejavior is always standard, so you can hardcode the expectation in your mind, can't you.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it means I can't predict what a method will do by looking at the parent implementation even if I desperately *want* the parent implementation. I might be holding an evil child.
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all who replied, but Ernest's post, "Even before an object is fully constructed, method calls will always go to the most derived version of a method", confirmed what I suspected.


Dan
[ November 09, 2005: Message edited by: Dan Bromberg ]
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Selikoff:
Because it means I can't predict what a method will do by looking at the parent implementation even if I desperately *want* the parent implementation. I might be holding an evil child.


True. I wonder if there is any way of definitively calling the parent implementation.

Maybe a new syntax like this



Returns java.lang.Object@10b62c9 not abcd
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic