aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Parent constructor calls method of Child Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Parent constructor calls method of Child" Watch "Parent constructor calls method of Child" New topic
Author

Parent constructor calls method of Child

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
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.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
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

Joined: Aug 24, 2003
Posts: 270
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

Joined: Oct 17, 2003
Posts: 150
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

Joined: Oct 17, 2003
Posts: 150
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

Joined: Aug 24, 2003
Posts: 270
Thanks Harwinder, your explanation was great.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
I'm glad to be of any help
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Aug 24, 2003
Posts: 270
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

Joined: Mar 05, 2003
Posts: 1391
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Parent constructor calls method of Child