• 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

Super class constructor

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above code example, it is stated in the book that the super constructor will be called by the constructor at line 5 rather than the one at line 9 (See page 142 of SCJP 6). I dont quite see how this is possible. Wouldnt the compiler implicitly add a super() clause on the constructor at line 9 when the constructor is called?

Thanks
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i looked at the rules again on the previous page and i think that the constructor will not generate a super() statement if the first line of the constructor is a call to this().

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Dude, the super class is always called. Irrespective of what the first statement of the constructor is. I modified the code to show it to you.



Output:

selva
Spike
selva
Zeus
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No. Dude, the super class is always called.


No its not. If your constructor calls another constructor of the same class using this then the compiler doesn't insert a call to super class constructor. You can create a class like the following which will never call the super class constructor (of course this class will also result in a StackOverflowError as the calls to the constructors will continue recursively forever)
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes , its correct . if first line of any constructor is calling to any version of this() , then compiler will not generate call to super() , it will only call to super() if there is no call to any other constructor in first line of any constructor.

worth noting is compiler always called no arguments super() , so if your super class doesn't have no args constructor and you will not call super(args) explicitly you will get compiler error.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you explain the output for the above code, then?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The super class constructor is called if you don't call another constructor of the same class yourself. In your code the Animal(String) constructor will call the super class constructor. Since both the instances of Animal class you create end up calling Animal(String), so the super class constructor is called both times. The example that I gave was an extreme one (which results in an exception at runtime) to show that the super class constructor is not always called as the first statement of a constructor. If you modify your constructors like this, you'll understand when the super class constructor is called
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anikt Garg wrote:No its not. If your constructor calls another constructor of the same class using this then the compiler doesn't insert a call to super class constructor.


I slightly disagree with this, i think super class constructor has to get invoked some how , may be of different version as you keep chaining this(---) calls ....else it would end up in recursive call giving you error...
The code you have posted gives me compilation error in 1.5 for Recursive Constructor Invocation...
Let me know if i'm missing anything here
Would you please post some code which would bypass super class constructor ?
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ankit -
I would like to see how can we completely avoid call to super constructor.. unfortunately I could not find your second code snippet working
One request:: please post some running code so that i would understand your point
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He He, I feel old today. I was surprised the compiler detected the recursive constructor call as I was sure it compiles fine. And I found here that since JDK 1.4.1 the compiler detects recursive constructor call. I must've run such a code a long time back on JDK 1.2 or something, so basically I'm wrong (post JDK 1.4.1 world). Anyway the statement made by Ziggy is right not wrong

O. Ziggy wrote:the constructor will not generate a super() statement if the first line of the constructor is a call to this()


Rohit Ramachandran wrote:No. Dude, the super class is always called. Irrespective of what the first statement of the constructor is.


I was trying to prove that the super class constructor is not called from a constructor which calls another constructor using this which is still true. If the first statement of a constructor is a call to this(), then a super() call will not be there in that constructor...
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problemo Ankit ... Thanks a lot, I was not knowing that Recursive calls we allowed by Compiler prior to 1.4
To conclude if I say no matter what you do, it is guaranteed that at least one version of the base class constructor would be invoked, would it be incorrect ?

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To conclude if I say no matter what you do, it is guaranteed that at least one version of the base class constructor would be invoked


That would be correct...
 
Oh. Hi guys! Look at 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