Inheritance in Java - Creating an Object of the Subclass Invokes Also the Constructor of the Superclass. Why Exactly

Why is constructor of super class invoked when we declare the object of sub class?

Because it will ensure that when a constructor is invoked, it can rely on all the fields in its superclass being initialised.

see 3.4.4 in here

Does a superclass instance also being created when creating a subclass instance?

When an instance of a derived class is created the heap allocation will be something like (*):

  • Standard JVM object header (with pointer to Class object for DerivedClass)
  • Instance fields for class Object
  • Instance fields for class BaseClass
  • Instance fields for class DerivedClass

So in effect, if you ignore the DerivedClass instance fields the object looks remarkably like an instance of BaseClass, and the JVM can reference the object as if it were an instance of BaseClass and have no difficulty doing so.

Similarly, in the Class object for DerivedClass is a "virtual method table" with:

  • Virtual method pointers for Object
  • Virtual method pointers for BaseClass
  • Virtual method pointers for DerivedClass

The JVM makes virtual calls by indexing into this table to find a specific method, knowing that, say, hashValue is method number 5 and printTheGroceryList is method number 23. The number needed to call a method is determined when a class is loaded and cached in the method reference data in calling classes, so calling a method is: Get the number, go the the Class object pointed to by the instance header, index into the virtual method table, pull out the pointer, and branch to the method.

But when you look closely you will see, eg, that the pointer in the Object group that points to the hashValue method actually points to the one in BaseClass (if BaseClass overrides hashValue). So the JVM can treat the object as if it were an Object, invoke hashValue, and seamlessly get the method in BaseClass (or DerivedClass, if it also overrides the method).

(*) In practice the instance fields may be intermingled to a degree, since "aligning" fields from a superclass may leave gaps in the heap allocation that fields from a subclass can fill. This is simply a trick to minimize object size.

Does object creation of subclass create object of superclass, if yes Is it possible to access it in subclass?

Since whole inheritance hierarchy gets instantiated when we create a subclass objects, I was wondering, is it possible to access object of class ClassA in client class?

This is something lot of people get confused. If you create object of subclass, that does not mean it create object of super class.

Its just a call to constructor of super class, just to ensure that all the required fields are initialized in super class, but this does not create object of super class.

This question will help you, understanding the concept.

Check Kevin's answer:

It doesn't create two objects, only one: B.

When inheriting from another class, you must call super() in your constructor. If you don't, the compiler will insert that call for you as you can plainly see.

The superclass constructors are called because otherwise the object would be left in an uninitialized state, possibly unbeknownst to the developer of the subclass.

Is object created every time a constructor is called in java?

A constructor call doesn't create a new object, it initializes a newly created object.

When you create an Apple object with new Apple(), a single object is created. The initialization of the properties of that object involves executing multiple constructors (Apple, Fruit and Object).



Related Topics



Leave a reply



Submit