File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why a call to the parent's constructor ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why a call to the parent Watch "Why a call to the parent New topic
Author

Why a call to the parent's constructor ?

Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
Hi,
Can anyone tell me why this program calls Super's constructor, although there is no call made to it by super() in Sub's constructor ?
class Super {
Super(){
System.out.println("In Super constructor");
test();
}

void test() {
System.out.println("In Super.test()");
}
}
class Sub extends Super {
Sub() {
System.out.println("In Sub constructor");
}

void test() { // overrides test() in Super
System.out.println("In Sub.test()");
}
}
Output if Sub sb = new Sub() is invoked:
In Super Constructor
In Sub.test()
In Sub Constructor
Thanks,
Stephane
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Stephane
The compiler automatically places super() as the first line in every constructor that doesn't beguin with super(...) or this(...)
This is so to ensure that the constructors of all the classes above in the hierarquy up to Object, are executed before the constructor of the class we are instantiating. In this way the fields that are inherited by the new object are alredy initialized to their proper value for the parent constructors.
You can read more:
Initializing the base class
Member initialization


SCJP2. Please Indent your code using UBB Code
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Stephane,
Constructor of Super() class calls overridden(newest,last) version of test() method.
Please read JLS 8.4.8 Examples of Method Declarations for additional info.
Thanx,
Jamal Hasanov
www.j-think.com
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Because a subclass is a precision of a superclass.
So you begin to call the constructer of the superclass, and then you call the constructer of the subclass . This method ensure that all objects are proper constructed and have all the fields and method needed. The call to super() is hidden.


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
You can read all about the nitty-gritty details of object initialization in the JLS, §l12.4.2 Detailed Initialization Procedure.
Corey


SCJP Tipline, etc.
Brett Swift
Ranch Hand

Joined: May 22, 2002
Posts: 61
What would happen if you made a call to this() in the Sub Constructor, would it still make an implicit call to super()?
I ask this because you can't have this() and super() in the same constructor.....
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Brett Swift:
What would happen if you made a call to this() in the Sub Constructor, would it still make an implicit call to super()?
I ask this because you can't have this() and super() in the same constructor.....

Yes, the call to super would still occur, but it would occur in the constructor that you invoked, not the one that you used "this" in.
Corey
Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
Thanks all for your (quick) answers.
I will say it again later I'm sure, but this site is really wonderful. No matter what question you have on SCJP, you always get precise (and correct )answers,
Thanks again,
Stephane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why a call to the parent's constructor ?
 
Similar Threads
constructor
Constructors.
overriding question
Unable to run the following program
shadowing variable - why the output is null