File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Howmany no of instances created in the inheritance tree? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Howmany no of instances created in the inheritance tree?" Watch "Howmany no of instances created in the inheritance tree?" New topic
Author

Howmany no of instances created in the inheritance tree?

Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Hi all,

Please let me know that how many instance will be created in the inheritance tree if we create the instance of the subclass?

e.g.

class A{}
class B extends A{}
class C extends B{}

1) here if we create the instance of class C (C objC = new C()) then how many instance will be created?
2) if it'll create only 1 instance of class C then why it'll not create the instance of the base classes A and B eventhough it'll call the constructors of the base classes?

Thanks.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

It will create 1 C object, which is also a B object and an A object (C IS-A B, B IS-A A).

A constructor doesn't create a new object. It initialises a new object that is created by the new operator. The A and B constructors will be called to carry out the initialisation associated with those classes.
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

Thanks Brown!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Howmany no of instances created in the inheritance tree?
 
Similar Threads
OVERRIDING ISSUES
Do constructors create objects? (was: is it true?)
extending class clarifications
operator instanceof confusion
is it create object of Base class?