Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor chaining - why?

 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,

I understand that when a new object is created ..

ex: Horse h = new Horse(); // p. 128 & 129 in K&B

Java calls the constructors of every superclass in its inheritance tree. (In this example that would be the constructors of both Animal and Object.)

My question is ... why? As a programmer, my intent when I create a new Horse is to create one object not three? Doesnt this cause a performance penalty if the inheritance tree is large? I'm curious about the reasoning behind this approach? Is it so that superclass methods are available?

kf
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to Inheritance, without having a parent class object how could you ever think of your child class object possessing the characteristics of parent class? Wouldn't it make the whole point of Inheritance go waste?

Though you may use it or not, but it IS assumed that the super class object(s) should be available for the subclass object(s) in the inheritance hierarchy.

Does that quench your thirst Keith?

Of course, a good question . Way to think and understand the concepts!
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan,

Thanks for the reply .. and yes this helps ... I do find it interesting that creating one child brings the entire family ancestry to life as well.

kf
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the whole point! You got it right here
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify: when you instanciate a Horse, only *one* object is created - a Horse object.

But consider this: the Horse class inherits every part of the Animal class, but the Horse constructor doesn't know how to initialize those parts. Only the Animal constructor knows how to do that. So the Horse constructor calls the Animal constructor to initialize the Animal parts of the Horse object.

Does that help?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding on top of what Ilja said, we let the *individual classes* take the responsibility of preparing its own members to be available for the subclasses when any of the subclasses are instantiated.

This is achieved through "Constructor Chaining"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic