• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor invocation

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to know from which line the super class constructor will be called.....from 11. or from 17. And if you figured out then let me know,
how did you figure out?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:
I want to know from which line the super class constructor will be called.....from 11. or from 17. And if you figured out then let me know,
how did you figure out?



The super() constructor will be called from the Overloaded constructor of ConstructorInvocation. As the default constructor of ConstructorInvocation has a call to this(int) so the compiler will not add a call to the super(), instead the overloaded constructor of ConstructorInvocation will have an implicit call to the super().
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you prove that, through some code ...

mohammed sanaullah wrote:

swaraj gupta wrote:
I want to know from which line the super class constructor will be called.....from 11. or from 17. And if you figured out then let me know,
how did you figure out?



The super() constructor will be called from the Overloaded constructor of ConstructorInvocation. As the default constructor of ConstructorInvocation has a call to this(int) so the compiler will not add a call to the super(), instead the overloaded constructor of ConstructorInvocation will have an implicit call to the super().

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:can you prove that, through some code ...


The code you provided is the proof
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I would say the super class constructor has been called form line 11 then also output will be the same as what we got from line 17...that's
why i was asking...?

mohammed sanaullah wrote:

swaraj gupta wrote:can you prove that, through some code ...


The code you provided is the proof

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Swaraj, This is a simple example with couple of rules. Please go through K&B. Also, we are not debating to prove it. Please do not ask like it. Your previous replies to my questions were very poor, incorrect and mislead. Please remember that you are dealing with some great people including authors. That's why I wanted to send a private message but you did not respond. Coming back to your question,
Line 12(this(7)) is calling Line 16. At the end of Line 17 has implicit call (super(), without our knowledge compiler generates one automatically) which will talk
to super class constructor which is Line 3. After completing the super class constructor (Lines 3-5), control will come back to Line# 18, execute it and return
back to Line 13 and completes the execution. K&B has very very detailed explanation on this topic. Please feel free to revert back if you are not clear at any point.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:If I would say the super class constructor has been called form line 11 then also output will be the same as what we got from line 17...that's
why i was asking...?



Did you try? super() or this() have to be the first statements in the constructor (if at all they are mentioned). That way you cannot have both this and super calls in the constructor. If the super() had been called from the Default constructor of the Sub class- Then After completing the execution of the super() constructor, the control would come back to the sub class's Default constructor. In that case the overloaded constructor in the Sub class would not have been called.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again i am extremely thankful to you for your reply that "we cant have super() and this() in the same constructor as both of these must be the first statement of the constructor...", though i already knew this fact but actually it didn't click me...thank you very much..and please don't think that i am testing others knowledge here as Mr.Harikrishna Gorrepati said...I was having that doubt and thats why asked it...

mohammed sanaullah wrote:

swaraj gupta wrote:If I would say the super class constructor has been called form line 11 then also output will be the same as what we got from line 17...that's
why i was asking...?



Did you try? super() or this() have to be the first statements in the constructor (if at all they are mentioned). That way you cannot have both this and super calls in the constructor. If the super() had been called from the Default constructor of the Sub class- Then After completing the execution of the super() constructor, the control would come back to the sub class's Default constructor. In that case the overloaded constructor in the Sub class would not have been called.

 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Harikrishna, i really would like to know which of my reply mislead you....so that if i am wrong i can correct myself...

Harikrishna Gorrepati wrote:Dear Swaraj, This is a simple example with couple of rules. Please go through K&B. Also, we are not debating to prove it. Please do not ask like it. Your previous replies to my questions were very poor, incorrect and mislead. Please remember that you are dealing with some great people including authors. That's why I wanted to send a private message but you did not respond. Coming back to your question,
Line 12(this(7)) is calling Line 16. At the end of Line 17 has implicit call (super(), without our knowledge compiler generates one automatically) which will talk
to super class constructor which is Line 3. After completing the super class constructor (Lines 3-5), control will come back to Line# 18, execute it and return
back to Line 13 and completes the execution. K&B has very very detailed explanation on this topic. Please feel free to revert back if you are not clear at any point.

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:Once again i am extremely thankful to you for your reply that "we cant have super() and this() in the same constructor as both of these must be the first statement of the constructor...", though i already knew this fact but actually it didn't click me...thank you very much..and please don't think that i am testing others knowledge here as Mr.Harikrishna Gorrepati said...I was having that doubt and thats why asked it...



Dont worry Swaraj, we are all here to learn and teach
One suggestion for your SCJP preparation- Try to write some sample programs (like the one you posted here) if you have some doubts. It will help you a lot
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please click "My Profile" link on this page and choose "Yes" radio button for Allow other users to send private messages: Then, I will walk you through.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i did that...

Harikrishna Gorrepati wrote:Please click "My Profile" link on this page and choose "Yes" radio button for Allow other users to send private messages: Then, I will walk you through.

 
Sheriff
Posts: 9707
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
Harikrishna, we appreciate people to use the forum not email or PM. Answering a question via PM only helps one person, answering it in the forum will help anyone who gets confused over the same thing...
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From line 11.
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic