• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor chaining

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your answers - it's much appreciated!

Also thanks for the welcome!
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic