• Post Reply Bookmark Topic Watch Topic
  • New Topic

super class objects.  RSS feed

 
subho saha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a subclass which extends a superclass,and then if i want to instantiate the subclass then the costructors of the all the super classes upto the hierarchy "Object" class are called implicitly or explicitly as we all know. But my question is are objects(may be anonymous) of all those super classes are created at that time i.e on instatiating a sub class are objects of all it's super class is created?
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, 'Objects' of all the superclasses are not created. Only the constructors(and whatever calls they make) of all the superclasses are called in 'top-down' fashion.

The reason why this is done is because the sub-class object may be referring to/using some variable declared in the superclass. Also, the total memory requirement of the object to be created depends on the superclasses(variables declared by them and used by the sub-class, as above).


So, the reason why the constructors of all the superclasses are called is to gather all the information required to create an object of the subclass. Creating an object of all the superclasses is not necessary and does not serve the above purpose and hence is not done.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any instance of a subclass is also an instance of the superclass. So in order to construct a subclass object, a superclass constructor must be called. For example, consider this hierarchy:

class Animal {...}
class Cat extends Animal {...}

Now, if I'm constructing a Cat, I'm also constructing an Animal, because a Cat is an Animal. But this does not mean that there's a "separate" Animal created. It simply means that creating an Animal is part of creating a Cat.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you instantiate your subclass into a reference variable it has within it the superclass it extends and then the superclass has in it the Object class which is the superclass of all objects. So I guess all three are instantiated at the same time.

All the objects will live in the heap.
[ September 03, 2005: Message edited by: Megs Maquito ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Megs Maquito:

All the objects will live in the heap.


You're missing the important point: there's just one object. If a superclass has member variables a and b, and the child declares members c and d, then the child object is a single object holding members named a, b, c, and d.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!