Inheritance of constructors in java

Whenever any constructor is called in any derived class the task gets accomplished only by eventually calling the base class constructor implicitly or explicitly (correct me if I am wrong here).

Since we intended to create an instance of the derived class, but since the base class constructor is called in the end.

So, how is an instance of the derived class constructed despite the constructor of base class being called upon?

Jon Skeet
people
quotationmark

Don't think of the constructor as creating the instance. Think of it as initializing the instance, with respect to that particular class.

So the initialization process looks something like:

  • Allocate memory
  • Initialize object from perspective of java.lang.Object
  • Initialize object from perspective of your.package.Superclass
  • Initialize object from perspective of your.package.Subclass

(Even though you start with a call to new Subclass(...), the superclass constructor body is executed first.)

The details of object initialization are given in JLS section 12.5.

people

See more on this question at Stackoverflow