Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private field of parent initialised in child instance?

 
Daniel Clinton
Ranch Hand
Posts: 46
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
While thinking about object initialisation a question came into my head:

Is a private field of a base class initialised (although generally invisible) in a derived class instance?

I spent a bit of time pondering/coding this and so far it seems to be the case
but I'm not certain yet
Here's my thoughts so far.
In a class with a private instance variable, access is possible through another accessible member, like with this example:

Super s =new Super();
s.aMethod();
will create an instance of the class Super and then invoke aMethod()
which accesses the private field from within a constructed instance.
Ok, no problem.

But what exactly happens when a child class method calls super.aMethod like this:


There's no instance of Super this time just Sub.
But the private field is clearly initialised.
Does the first line of a child's constructor (the call to a super constructor)
initialise even private parent fields within a child object
although they remain invisible directly?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

You can NOT create an instance of a child class WITHOUT invoking the constructors of ALL its parent classes.

So for your example:


the following constructors are invoked (in this order)
Object
Super
Sub


Why Object? Because java.lang.Object is the parent class of each class, directly (when you don't have an extends-clause) or indirectly (through the parent classes). So both class declarations are exactly the same, but the 1st one is less work


All the following statements are true:
  • Super IS-A Object
  • Sub IS-A Super
  • Sub IS-A Object


  • A constructor of a child class will run after all the constructors of its parent classes have finished. But in your example there seems to be no constructors at all. So which one is executed? The Java compiler is a very friendly and helpful guy: when you don't provide a constructor, the compiler adds a default no-arg constructor for you. This constructor will ONLY be added if you don't add a constructor yourself.
    You write the very short and simple:

    When the compiler is finished that code looks like:

    So the compiler adds a no-arg constructor with a call to the constructor of its super class (class Object in this case) for you: Super() { super(); }

    Let's see what happens if we add a constructor to the Super class to initialize the fieldOfSuperOnly instance variable. Will this code still compile or not? And if not, which modifications should be made?


    Hope it helps!
    Kind regards,
    Roel
     
    Daniel Clinton
    Ranch Hand
    Posts: 46
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,
    Thanks for your very clear explanation. My query wasn't really this one but
    I've figured out now where my understanding was off-course.
    I was ok with the constructor chaining through line of class descent.
    Except I had this basic flaw in my mind-picture:
    I was picturing instance fields with a private modifier as being somehow
    left out of the sub class initialisation of the super class.
    Of course, they are not. It's the ACTUAL constructor initialisation code that runs
    A quick javap diasassembly of Sub
    showed me that it is an actual initialisation of the parent
    (ie: the JVM created <init> method) as the first line in all constructors
    and not any newly created, specially tailored initialisation.

    In my example:The class Sub does not and cannot access a private field in its parent
    (Even though that field is there pushed onto the stack in the Super."<init>" code)
    But the overridden method super.aMethod is available in the Sub class and it does the accessing
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Daniel Clinton wrote:The class Sub does not and cannot access a private field in its parent
    (Even though that field is there pushed onto the stack in the Super."<init>" code)
    But the overridden method super.aMethod is available in the Sub class and it does the accessing

    Spot-on!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic