This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Subclass calls super class constructor then how many object will be created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Subclass calls super class constructor then how many object will be created" Watch "Subclass calls super class constructor then how many object will be created" New topic
Author

Subclass calls super class constructor then how many object will be created

Amitt Thakkarr
Greenhorn

Joined: Apr 25, 2011
Posts: 7
Hi,
If Sub class calls the super class constructor. Then if i create the object of subclass then how many object or memory will be created.
suppose ClassA extends ClassB, ClassB extends ClassC, ClassC extends ClassD
i created the object of ClassA(new ClassA()) then how many object will be created.

Thanks,
Regards,
Amit
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

Hi Amitt. Welcome to The Ranch!

Just one. That one object IS-A ClassA, ClassB, ClassC and ClassD.

(Moving to Beginning Java)
Amitt Thakkarr
Greenhorn

Joined: Apr 25, 2011
Posts: 7
Hey Mathew.. Thanks..
But i wanted to know if subclass will call the super class constructor by default then how memory created for each constructor...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

It will call the superclass constructor, but only to carry out any initialisation that the superclass requires. It doesn't create another object. Constructors don't directly create objects; rather, all the relevant constructors are called when an object is created.

How much memory? It depends on what the superclass constructor does. Possibly none.
 
 
subject: Subclass calls super class constructor then how many object will be created