• 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

question on object creating

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there, i've been reading K&B and among other questions i have this one:

K&B

You may see the words "construct", "create", and "instantiate" used
interchangeably. They all mean, “An object is built on the heap.” This also implies that
the object’s constructor runs, as a result of the construct/create/instantiate code. You can
say with certainty, for example, that any code that uses the keyword new, will (if it runs
successfully) cause the class constructor and all superclass constructors to run.



Ok, so given that i have classes A, B extends A, C extends B, if i say there will be three objects created on the heap (since constructors for A and B will also be called), not just the C object. Is that right?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miltos Deligiannis wrote:Ok, so given that i have classes A, B extends A, C extends B, if i say there will be three objects created on the heap (since constructors for A and B will also be called), not just the C object. Is that right?



No. You only call the new operator once. Only one object is created.

On the other hand, the C instance (which IS-A B, and IS-A A) may contain other objects -- which are also instantiated as part of the instantiation of the C object.

Henry
 
Miltos Deligiannis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks. So, based on that, constructor calls (through inheritance) does not reflect the number of objects on the heap...
 
Please do not shoot the fish in this barrel. But you can shoot at 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