aspose file tools*
The moose likes Java in General and the fly likes Why Oject class constructor is called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Oject class constructor is called?" Watch "Why Oject class constructor is called?" New topic
Author

Why Oject class constructor is called?

rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82
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

Joined: Oct 20, 2005
Posts: 257

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

Joined: Jan 13, 2006
Posts: 82
I didn't understand.

Can any one kindly explain more detaily.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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?


[Jess in Action][AskingGoodQuestions]
rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82

So, are you asking why this is so


Yes.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.
 
subject: Why Oject class constructor is called?