• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unexpected result in constructor call

 
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic