aspose file tools*
The moose likes Beginning Java and the fly likes object creation behind the scenes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object creation behind the scenes" Watch "object creation behind the scenes" New topic
Author

object creation behind the scenes

Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
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

Joined: Oct 12, 2000
Posts: 5093
simply because there is no parent object.
The superclass constructor is called on the subclass object, not on an implicit superclass object.


42
Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
О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

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
ОК, cheers mate,

That's another bit put into place in this Java puzzle.... ))))))))))
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
Thanks Mike!
Great stuff.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object creation behind the scenes