• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

constructor chaining

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor calls its parent class constructor and thus for every object creation constructor of class Object is called.
Does this mean a class lower in inheritance tree has more memory overhead?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a subclass doesn't define any instance fields, I can't imagine why it would require more memory for each instance that its superclass's instances.
 
Krazee soul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what do they really mean by superclass constructor is invoked?Does that not mean superclass object is created?An object at depth three in inheritance tree will need three objects created and at depth six will need
six objects created?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's more like a single compound object, but there is definitely more overhead data stored for a subsubclass than for a childless class that directly extends Object.

Remember that the private members of a parent, while hidden from its child, still exist in the memory structure of a child instance.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, that's the second time today I've read here that someone thought each class along the chain of inheritence was a separate object. I wonder if it's being taught that way.

As Mike said, each instance of any class is a single object. When you instantiate class B that extends class A that extends Object, one single chunk of memory is allocated for the single instance of B. That memory will hold the instance fields for Object, A, and B plus the standard instance overhead (at least the instance's Class).

When each constructor is called in the chain, they only initialize the fields of the memory block -- the memory has already been allocated.

Mike, I meant that if class B above defines no fields, then its memory block will be the same size as an instance of class B. I could easily be mistaken on that, though, but I can't think of what it would need to add.
 
Screaming fools! It's nothing more than a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic