aspose file tools*
The moose likes Beginning Java and the fly likes How does inheritance work underneath? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does inheritance work underneath?" Watch "How does inheritance work underneath?" New topic
Author

How does inheritance work underneath?

Mike Stach
Greenhorn

Joined: Oct 26, 2008
Posts: 8
I have a basic question related to inheritance.
Consider the following code snippet:

public class B {
}

public class D extends class B{
public D(){
super();
}

public static void main(String args[]){
new D();
}
}

My question is whether new D() invocation will create 3 objects i.e. D, B, and top class Object or whether it will create only 1 object i.e. D?

I am confused as to whether inheritance leads to chained object creation or whether the compiler copies all the eligible public and protected members from the base class into the derived class so that base class does not need to be instantiated all the time.

When I see the bytecode, it looks like 3 objects are created because of init() invocations but I just wanted to be sure about it. I would appreciate if someone could comment on it.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I think the confusion is because the super class constructors are called. Only one object(the final one in the inheritance tree) is created. It would be better if you could try to understand the "IS-A" relationship. When you create a 'Dog' you are also creating an 'Animal', because Dog is an Animal. You are creating a SINGLE object that is instance of both a sub and superclass.


Cheers!
RSR
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

Horse Rider, welcome to JavaRanch. Please check your private messages for an administrative matter. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Horse Rider:

I am confused as to whether inheritance leads to chained object creation or whether the compiler copies all the eligible public and protected members from the base class into the derived class so that base class does not need to be instantiated all the time.


Neither the first nor the second.

It's the Virtual Machine that, at object creation time, will create *one* object that has all the members of the whole class hierarchy.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How does inheritance work underneath?