• 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

Parent constructor calls method of Child

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javaCertficate Q.

Output:
Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device
I dont understand how Phone() constructor has access to the method of Mobile. Thanks.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy
Class Mobile overrides the showDevice() method of class Phone. Since the object being created is of class Mobile, Dynamic Method lookup will cause Mobile's showDevice() method to be executed in each case.
Hope that helps.
Cheers
Harwinder
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harwinder,
Mobile constructor --> Phone constructor --> showDevice()
Now constructors are not inherited, I just dont get how the Parent class can see the showDevice() method of Mobile.
I think I am getting too stressed out..
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy
You have a reason for confusion and I had the same confusion a few days back.
I never said that the constructor was inherited. We are talking about overriding the showDevice() method.
Now tell me what object is the showDevice() method in the Phone constructor being called on? Answer is 'this'.
And what object is being created here? Answer is Mobile class object.
So, what could 'this' reference possibly refer to in this example? Answer is Mobile class object.
So, basically, the Phone class constructor calls the showDevice() method on the Mobile class object. And if the Mobile class had "not" been overriding the showDevice() method in the Phone class, then the showDevice() in Phone class would have been invoked.
Does this make things a little more clearer? Do not hesitate to ask more questions.
Cheers
Harwinder
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy
One more point:
Notice that the showDevice method() in the Phone class is an instance method. So, showDevice() of the Phone class can't be invoked from the subclass reference (this) when the subclass is overriding it.
If the Phone() constructor were to invoke the showDevice() of the Phone class, then showDevice had to be declared static in the Phone class (ofcourse then you'd have to declare the showDevice() in the Mobile class as static too and remove the reference to instance variable 'device' from the showDevice() of the Mobile class).
Cheers
Harwinder
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Harwinder, your explanation was great.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad to be of any help
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good question indeed! Especially that null in the output. Mobile instance variable device is not initialized at that time yet because Mobile's constructor has not completed.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the way I think about it: In general, we have to consider what happens both at compile-time and at run-time.
When the compiler is compiling the Phone constructor and comes to the line where the method showDevice is invoked, the compiler has to find a method declaration with the same name and compatible parameter types.

This is important to remember, because the compiler might have to choose between overloaded forms, declared and inherited. The compiler does not look at overriding method declarations.
In this example, the compiler selects the method declaration in Phone.
At run-time, when it comes time for the virtual machine to execute the method showDevice, the virtual machine might execute the method implementation chosen by the compiler or it might execute an overriding method implementation.
It�s important to remember the method declaration the compiler chose, in case the method declaration is static or is non-static and private. Otherwise the virtual machine will look for an overriding method implementation instead.
In this example, the virtual machine is executing the Phone constructor and comes upon the instruction to invoke and execute the method showDevice. Because the class of the object is Mobile, the virtual machine decides to invoke the overriding method implementation in Mobile instead of the method implementation chosen by the compiler.
Anyway, that is how I explain to myself how a superclass constructor executes a subclass method instead of its own method.
[ November 06, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I draw pictures to see what is happening. When the virtual machine executes new Mobile(), memory is allocated for the fields of both Phone and Mobile. Although the object is of type Mobile, I draw an object on the heap showing both the Phone fields and the Mobile fields.

The Mobile constructor gives the Phone constructor a reference to the Mobile object. Before invoking the Phone() constructor, the Mobile() constructor loads the reference to the object onto the operand stack:

The Phone constructor gives the showDevice method a reference to the Mobile object. Before invoking showDevice(), the Phone() constructor loads the reference to the object onto the operand stack:

Even though we are in the Phone constructor, the virtual machine has a reference to the Mobile object.
invokevirtual means to use the dynamic lookup procedure. Look for the method implementation of showDevice in the class of the object.
[ November 07, 2003: Message edited by: Marlene Miller ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow Marlene ...Thanks a lot ...
your concepts are crystal clear.
Now I can clearly see how the Phone() object can access
Mobile()'s method. I overlooked the concept that when Mobile()'s
constructor calls Phone()'s constructor its pass a this object.
Thanks again.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am so Happy you see what I was trying to say. Thanks.
And thanks to Harwinder, who made me start thinking about "this" in the Phone constructor.
[ November 07, 2003: Message edited by: Marlene Miller ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic