Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Oject class constructor is called?

 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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?
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I didn't understand.

Can any one kindly explain more detaily.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

So, are you asking why this is so


Yes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic