File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does inheritance work

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java jQuery
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic