aspose file tools*
The moose likes Beginning Java and the fly likes How does inheritance work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does inheritance work" Watch "How does inheritance work" New topic
Author

How does inheritance work

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Hi guys, my question is, when you extend a class does your subclass get a copy of each and every variable that belongs to your superclass? Or does you subclass simply gain access to the variables that are inside the superclass(no copies)?
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
Memory is allocated for all the necessary items of any given class. So when your super class is declared, it will do the same, reserve whatever memory is necessary. With inheritence, this same thing occurs in addition to reserving memory for the sub class's components. It is not about copies or variables.

In addition, let us look at an example:




In this example the memory location for the variable foo in Base, is accessible in SubBase. They point to the same memory location, it is not a copy but rather a reference to the base.foo field.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

An instance of a subclass is like an instance of its superclass(es) with an extra layer on top. There is only one copy in memory of the member variables of the superclass - it's not so that for the subclass part there is a copy of the member variables of the superclass part somewhere.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Thank you guys for answering my question but there are few things i dont understand. When i said "copies" i meant the concept of objects getting their own copies of instance variables. According to you answers subclass doesnt get any copies but refer to the same memory location. But here's the problem


output

class A = 10
class B = 30

If x variable inside class A and class B are the same variable, not copies, then why doesnt changing the value of x using "b" reference variable, change the value of x inside A, doesnt that mean class A and B have their own copies of x. Please help me understand.
Frankely Diaz
Greenhorn

Joined: Oct 09, 2009
Posts: 20

You have to different Objects there thats why the values are not the same

Maybe this code is near to what you want to describe
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Oceana Wickramasinghe wrote:...
output

class A = 10
class B = 30

If x variable inside class A and class B are the same variable, not copies, then why doesnt changing the value of x using "b" reference variable, change the value of x inside A, doesnt that mean class A and B have their own copies of x. Please help me understand.


I think the other responder has the right answer, but an unfortunate spelling mistake. He said there are "to different objects", and meant "two different objects".

I am going to try my own explanation anyway.

When your code executes the statement "A a = new A();", that causes the runtime to create an object of class A; this includes allocating memory for the variables in that newly created object.

When you execute "B b = new B();", you create another object, this one of class B. This includes allocating memory for all of B's variables, some of which were declared in Class A, and are part of an object of class B because of inheritance. But the two different object allocations are not linked to each other. You could execute this second statement 3 times; it would create 3 objects of Class B, and all of their instance variables would be independent of each other. You could execute the first statement 3 times, giving you 3 objects of class A, and with the other 3 statements you would have 6 independent objects.

Does that make it any more clear?

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does inheritance work