• 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

Why Oject class constructor is called?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a class that dosen't extend any class. So it implicitly extends Object class. Now when my constructor is called it internally calls the Object class's constructor.

All I want to know is
why this(Object class's constructor) is being called.?
What benefits we get?
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The constructor is used to setup the initial object state or behavior. The state of current generation depends on the state or condition of the previous generation.

In inheritance , the state of the child object always depends on the parent object. The state of the parent object is required to setup before setting up the child state.

More inputs are welcome.
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand.

Can any one kindly explain more detaily.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rama murthy:
I didn't understand.



I'm afraid I don't understand your question, either.

In Java, when an object is created, its constructor is always called. Every constructor calls the direct superclass's constructor, and so on, up until Object's constructor is called. This way, every object, including the parts inherited from some other class, can be initialized.

So, are you asking why this is so, or are you saying (for some reason that's unclear to me) that you think Object should be a special case, and its constructor should be skipped?
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So, are you asking why this is so



Yes.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rama murthy:

Yes.



Because every class may need a chance to initialize its member variables; this is done in the constructor. Therefore the constructor of each parent class is called, so that any inherited member variables may be initialized.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic