• 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

Calling constructor from another constructor

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to know, why we cannot call the same class contructor from another constructor of the same class along with calling a super class constructor. what may be the rationale behind it.

For ex.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nitinram agarwal wrote:Hi,
I would like to know, why we cannot call the same class contructor from another constructor of the same class along with calling a super class constructor. what may be the rationale behind it.
[/code]



The reason is because the compiler won't allow you to call the constructor of the super class twice. Hence, it will either allow you to call the super() constructor (as the first line), or the this() constructor (as the first line). If you use the this() constructor, the super() constructor will actually not be called (no implicit call), because it will be assumed that the other constructor will do it.

In your case, you can't call both the super() and this() because this will attempt to construct the super class twice. The other constructor doesn't have a call to either super() or this(), and hence, has an implicit call to the super() no-arg constructor.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic