This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help explaining some results

 
Cliff DeRose
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a little test program, but I was surprised at the result that I got. Can anybody explain to me why the first line of the results for this program is: amethod in Y.

Thanks,
Cliff
[ January 08, 2004: Message edited by: Cliff DeRose ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure, since you haven't pointed how the results confuse you, but I'm guessing it's the same as the thread here.
You can also use the UBB CODE tags to format code.
Dave
 
Cliff DeRose
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result I got from running this program was:
amethod in Y
10
amethod in Y
This was not what I expected though. Maybe I'm overlooking some wierd polymorphism rule here?
I thought the results would be:
amethod in X
10
amethod in Y
My question is why the first result happens and not the second (the one I expected). Its clear that when a new object of type Y is being created, in its constructor it calls its superclass constructor. But, in the superclass constructor, why does it not use the amethod defined in the superclass instead of the one defined in the derived class? I hope that makes my issue a bit more clear.
Thanks,
Cliff
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the Java Language Spec in relation to instance creation (refer to section 12.5). To quote:

Unlike C++, the Java programming language does not specify altered rules for method dispatch during the creation of a new class instance. If methods are invoked that are overridden in subclasses in the object being initialized, then these overriding methods are used, even before the new object is completely initialized.

It then gives an example similar to yours showing that even though you are in a constructor, there is an implied "this" which is an instance of class "Y", so that classes method is invoked.
It was a decision made early on and it works that way because the creators of Java explicitly specified it to be so.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cliff DeRose:
The result I got from running this program was:
amethod in Y
10
amethod in Y
Thanks,
Cliff

This is because of late binding. When you create delare myX as as X but then define myX as new Y, java compiler doesn't do the binding (that would be early binding). It gets resolved at run time; at run time it checks to see the type of object myX is & then calls the constructor of Y.
 
Cliff DeRose
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much, it is much more clear now. Seems like just the kind of a sneeky question that would appear on the sun certified java programmer exam.
Cliff
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic