• 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

can we have two constructors being called for the same object ???

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

can we have two constructors geeting called for the same object
i have a piece of code please help me understand here that when the PrivateConstructor(int i) constructor is called, is the super() constructor called at that very moment or first this() constructor is called and from there super() is called


thanx
amit
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First this() [ constructor in same class with no parameter ] is getting called then from there super() [ constructor in direct super class with no parameter ] is getting called .

The output will be :

in the topmost level
in :1
into:
2
[ March 22, 2005: Message edited by: rathi ji ]
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah rathi...
ur right...i think whenever we have a situation like this whichever constructor is called first is executed last, whatever might be the case...
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rathi,

I do not understand as to why the constructor of the super class ("in the topmost class") is called.

We are not initializing it explicitly (or is it implicit?).

I am not sure if the constructor is invoked even if an instance of tht class is not created (using new) and simply by extending the super class.

Thanks

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

Originally posted by kiennjal shah:
Hello Rathi,

I do not understand as to why the constructor of the super class ("in the topmost class") is called.



Kiennjal,
Whenever you create object of a class, the constructor of the topmost super class in the hierarchy is called first and then the constructor of it's child and so on... down the hierarchy.
Hope that answers your query.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kiennjal ,

Every construtor have either this(*) or super(*) at first line of their implementation . [ * = 0 or more parameters ] If you are not writing explicitly then compiler adds internally super() at first line .

A simple example is :

this is your code .


But for compiler it is :


your code :

for compiler :


your code :

for compiler it is :


hope it is clear now .
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Thanks, that made it quite clear!

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

Hello Rathi,
I do not understand as to why the constructor of the super class ("in the topmost class") is called. We are not initializing it explicitly (or is it implicit?).


But why JVM calls the the default constructor of the super class.

When ever a class inherits a parent, the child class has all the public/protected members of the super class ( parent). Constructor is the place where you initilize the members of a class, so the the members inherited from the super class has to be initilized ( atleast by default values like 0 , null etc etc ) so the default constructor is called by the jvm and this should be the first statement in the constructor.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic