• 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

Calling an object's non-static method before its constructor has run

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we are creatinga child that extends a parent, and both of them have a common (=overridden) method named CommonMethod,
this is what I think should be the sequence (shown in the code below):
1) JVM starts by starting the instantiation of Child
2) Since Child extends Parent, instantaition of Child is temporarily suspended
3) JVM starts the instantiation of Parent
4) Constructor of Parent is called
5) Parent's Constructor calls the method CommonMethod. This method exists in both Child and Parent
6) Here is the crux. I think that Parent's CommonMethod should be called. Execution however showed that Child's method was called instead!
7) And THEN, JVM instantiates (rather, completes the instantiation process of) Child

BUT: When I executed the below code , this is what happened:
(A) Parent constructor did get called first - as you would expect
(B) CommonMethod called used the "semi-existent" Child object's CommonMethod, not Parent's !
(C) AND THEN the constructor for the child object was called

Can someone please point out the error in my concepts?


public class Tester
{
public static void main(String[] args)
{
Child c = new Child() ;

}
}

class Parent
{
public Parent() { CommonMethod() ; }
public void CommonMethod() { System.out.println ("I am the Parent") ; }
}

class Child extends Parent
{
public Child() { CommonMethod() ; }
public void CommonMethod() { System.out.println ("I am the Child") ; }
}

So I expected that the output would be:
1) I am the Parent
2) I am the Child

The actual output was
1) I am the Child
2) I am the Child

Need to understand the 'real sequence of events', I guess
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First of all, there is only *ONE* object being instantiated -- not two. A child IS-A parent, the parent constructor is being called to instantiate the portion of the child that it inherited from the parent -- no separate object is being created.

To answer your question, polymorphism applies -- the overriden version of the method is always called. Even when instantiation of the object has not yet completed. This means that you have to be careful when calling methods during construction, as it is possible that the object is not yet in a ready state.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dilbert Adams wrote:
Need to understand the 'real sequence of events', I guess



Step Zero. Not really a step. Instance variables that are compile time constants are already instantiated. So, you can say they are done first (although technically they are constants and not variables).

Step One. During instantiation of a class, the super constructor is called first. This actually leads to that constructor's super constructor being called, and so on.

Step Two. Upon completion of the super constructor, the initialization of instance variables, and instance initializers are executed in the order that they are encountered in the source code.

Step Three. The rest of the constructor, after the super constructor call is done.

Henry
 
Dilbert Adams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. I was going wrong by not realizing that just like an instance var is initialized *before* the constructor is called, a method is actually just like a variable. Therefore it IS available before the constructor call is over !

Perfect - thanks again !
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic