• 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

Need help explaining some results

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic