aspose file tools*
The moose likes Beginning Java and the fly likes Unexpected result in constructor call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected result in constructor call" Watch "Unexpected result in constructor call" New topic
Author

Unexpected result in constructor call

Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 46
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


Thanks in advance to all who reply,
Dan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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."


[Jess in Action][AskingGoodQuestions]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
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.


ASCII silly question, Get a silly ANSI.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

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

Joined: Oct 18, 2005
Posts: 46
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

Joined: Oct 07, 2005
Posts: 637
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unexpected result in constructor call
 
Similar Threads
Difference between with class variable initialized or not initialized.
call back
Hoe can I create folders in any directory specified by me??
constructor
basic java question...please advice