File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why a call to the parent's constructor ?

 
Stephane Weber
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read all about the nitty-gritty details of object initialization in the JLS, §l12.4.2 Detailed Initialization Procedure.
Corey
 
Brett Swift
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic