This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes constructor chaining - why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "constructor chaining - why?" Watch "constructor chaining - why?" New topic
Author

constructor chaining - why?

Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

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


kf
SCJP 5.0 (preparing for SCWCD)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

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

Joined: Apr 20, 2006
Posts: 3344

That's the whole point! You got it right here
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: constructor chaining - why?
 
Similar Threads
How does inheritance work underneath?
casting classes and interface - need some clarification
over loaded methods??
Constructor Chaining
question of Round-up