Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance 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 "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
I have a question regading the following code,

The Output is:
Talking a lot
Talking a lot
My question is:
Why is the Parent Constructor being invoked ?
Why is the habbit() method which sits in the constructor of the Parent class invoking the habbit() method of the Child class ?
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
Why is the Parent Constructor being invoked ?
A call to the parent class default (no-arg) constructor is inserted automatically by the compiler into a subclass constructor if the subclass constructor is not calling one explicitly using super() (also assuming you're not calling another constructor of your subclass using this()) This is true whether you provide an explicit constructor or let the compiler provide a default constructor for you.
Why is the habbit() method which sits in the constructor of the Parent class invoking the habbit() method of the Child class ?
The choice of which habbit() method to call (parent or subclass) occurs at runtime based on the type of the object. In your case you created a Child object, and since the Child class overrides the habbit() method of the parent it's version is being called.
An interesting consequence of this is that any member variables defined in your child class will have only their default initialized values when the overridden method is called (since the child class constuctor hasn't completed running yet any code in the constructor that sets new values for the member variables won't have had a chance to run yet....)

will produce:
Talking a lot 0
Talking a lot 23

Note that the initial assigment of 19 to x isn't reflected in the first line of output and that the assignment of 23 to x IS reflected in the second line....
[ March 24, 2004: Message edited by: Richard Quist ]

Rich
SCJP 1.4
Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
Can someone explain why 0 is printed and not 19. I understand that x=23 hasn't been executed yet, but I would have thought that the member variable was initialized to 19 by this point in the program.
Brian
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
Originally posted by Brian Albin:
Can someone explain why 0 is printed and not 19. I understand that x=23 hasn't been executed yet, but I would have thought that the member variable was initialized to 19 by this point in the program.
Brian

My understanding is that the initializer code (x = 19) is essentially inserted by the compiler into the subclass constructor...so at the time the base class is constructed the assignment of 19 hasn't happened yet...
The subclass constructor that the compiler generates would look something like:

So at the time the superclass constructor runs and calls the habbit() method, the assignment of 19 hasn't happened yet....
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
To add to what Richard has said, here's what it states in the JLS, §12.5 Creation of New Class Instances:

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally. In the example:

If you can read through all that mumbo-jumbo you come out with this basic idea...
1. Invoke the constructor.
2. As the first command of the constructor, execute the constructor of the parent class.
3. Execute any instance initializers and instance variable initializers
4. Execute the remainder of the constructor body.
Granted, that's greatly simplified, but you get the idea. If you want the full details, they're quoted above in all their glory.
But, the end effect is basically what Richard said. It seems as if the instance initializers are "inserted" into the constructor immediately following the invocation of the parent constructor.
I hope that helps,
Corey


SCJP Tipline, etc.
Brian Albin
Greenhorn

Joined: Mar 02, 2004
Posts: 24
It helps greatly. Thanks guys.
Brian
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Thank you very much Corey and Richard.
Richard thanks for altering the code to accomodate an essential funda to know for the test.
 
jQuery in Action, 2nd edition
 
subject: Inheritance