Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object creation behind the scenes

 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was just playing around with simple java tricks like this one:

class Parent
{
public Parent(){
System.out.println("Parent's constructor");
System.out.println(this);
}
}

class Child extends Parent {

public Child(){
System.out.println("Child's constructor");
System.out.println(this);
}
}

public class Test {
public static void main(String[] args) {
System.out.println("Creating a child object");
new Child();
}
}


The output of this was as follows:

Creating a child object
Parent's constructor
Child@107077e
Child's constructor
Child@107077e


Here I caught myself wondering why we got a string representation of the Child object twice and not once the Parent and once the Child? I mean, I know it has to do with the implicit super() call in the Child constructor, but how come in the Parent constructor "this" turns out to be pointing at the Child, not at the Parent object?
[ November 25, 2004: Message edited by: Dmitry Danilov ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simply because there is no parent object.
The superclass constructor is called on the subclass object, not on an implicit superclass object.
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ОK, great, this makes things a bit clearer...

What about sureclass methods calls? They are also called on a subclass object, aren't they? How come then we sometimes do super.superclassMethod() (like super.invalidate() in Swing) if it's the same as calling this.superclassMethod() or simply superclassMethod()? Is it only applicable in situations with overridden methods when we specifically want to call superclass overridden method on a subclass object?
[ November 25, 2004: Message edited by: Dmitry Danilov ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dmitry Danilov:
What about sureclass methods calls? They are also called on a subclass object, aren't they? How come then we sometimes do super.superclassMethod() (like super.invalidate() in Swing) if it's the same as calling this.superclassMethod() or simply superclassMethod()? Is it only applicable in situations with overridden methods when we specifically want to call superclass overridden method on a subclass object?


I think that's about right, yes. By far the most common reason to call superclass method is when implementing an override of that same method. Your override often wants to do what the superclass does, plus a bit more.
[ November 25, 2004: Message edited by: Peter Chase ]
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ОК, cheers mate,

That's another bit put into place in this Java puzzle.... ))))))))))
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors don't construct anything. The chain of implicit or exlicit calls to super() and this at the beginning of you constructors set up a chain of execution in reverse order, beginning with Object().

After Object is reached, the one and only instance object is allocated based on information in the class object. Then the constructors execute downward from Object(), with each level preceded by the initializers and instance initialization blocks. So the this pointer in any constructor in the chain points to the same object.
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike!
Great stuff.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic