• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Why a call to the parent's constructor ?

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic