• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How does inheritance work

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Greenhorn
Posts: 29
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic