• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor chaining  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering if someone could offer me a little clarification on this.

I understand when you create an instance of a class the first statement called in the constructor either implicitly or explicitly is super().

Are you in effect creating instances and initialising each super class until you reach object itself and then it returns?

So when you instantiate a certain class theoretically you could be making instances of a load of other super classes. If these are all instances of different classes how are they linked? Or should I be thinking of all the different instances as being one big instance with a particular specialism?

Thanks,
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Jackson wrote:So when you instantiate a certain class theoretically you could be making instances of a load of other super classes. If these are all instances of different classes how are they linked? Or should I be thinking of all the different instances as being one big instance with a particular specialism?

You're not making a load of super instances. You're making one object that IS it's super classes and specialized in certain tasks.

And welcome to the JavaRanch
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Jackson wrote:...should I be thinking of all the different instances as being one big instance with a particular specialism? ...

Basically, yes. As Wouter pointed out, an instance of any subclass is also an instance of its superclasses. To illustrate this, suppose you have the following hierarchy:

Now suppose you create a new instance of Sportscar. First, (don't forget) Object's constructor is called to create a base Object. Next, Vehicle's constructor is called, and that Object becomes a Vehicle. Then, that Vehicle becomes a Car. And finally, that Car becomes a SportsCar. So you have one object, a SportsCar, that IS-A Car, and IS-A Vehicle, and (of course) IS-AN Object.

As Wouter also said, welcome to the Ranch!
 
Guy Jackson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your answers - it's much appreciated!

Also thanks for the welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!