• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Inheritance  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am not able to understand the final number (20) in the output.The output is: 20 20 40 40 20 20

b1.changeValue(): I understand how this produces 20 20. The derived class's chageValue() is called which sets value to 20. So both value and super.value are 20.

b2.changeValue(): Same as above. DerivedTwo's changeValue() is called, which makes both value and super.value 40.

b1.show(): Now, Derived class's value is still 20 as I expect because nothing changed since b1.changeValue() was last called, but I thought super.value would show 40 now because b2.changeValue() changed its value to 40 and Derived and DerivedTwo share the same super class. Please help me understand.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have time to dig in and figure it out, but remember - you can always put System.out.println statements in to see what's going on. "entering Base.show", "entering Derived.show", etc.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response, Fred. I did try that and I know exactly where each method call is going but I'm unable to understand the value of the variables being output.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason why you think it shouldn't be 20? Hint - The derived class does not hide the base class' value variable. So there is only one value variable.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b1.show(): Now, Derived class's value is still 20 as I expect because nothing changed since b1.changeValue() was last called, but I thought super.value would show 40 now because b2.changeValue() changed its value to 40 and Derived and DerivedTwo share the same super class. Please help me understand.


b1 and b2 are two different objects. They have their own, independent copy of value.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:
b1.show(): Now, Derived class's value is still 20 as I expect because nothing changed since b1.changeValue() was last called, but I thought super.value would show 40 now because b2.changeValue() changed its value to 40 and Derived and DerivedTwo share the same super class. Please help me understand.



The b1 and b2 reference variables are pointing to two different instances. And "value" is an instance variable. How can changing an instance variable of one instance, have any affect on the instance variable of another instance?

Henry
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heena Agarwal wrote:b1 and b2 are two different objects. They have their own, independent copy of value.

Thank you Heena. I think this is where my understanding is slightly flawed. I do understand that there are different copies of "value" for b1 and b2. But similarly, I thought there is one copy of "value" in the Super class which has been modified first to 20 and then to 40.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:How can changing an instance variable of one instance, have any affect on the instance variable of another instance?Henry

Thank you Henry. I think I'm struggling to understand how "super.value" still shows 20 after DerivedTwo changes it to 40.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:
Heena Agarwal wrote:b1 and b2 are two different objects. They have their own, independent copy of value.

Thank you Heena. I think this is where my understanding is slightly flawed. I do understand that there are different copies of "value" for b1 and b2. But similarly, I thought there is one copy of "value" in the Super class which has been modified first to 20 and then to 40.


For instance variables to be shared across objects, they have to be static. Your int value variable is not static. Hence it is not shared among different Base class objects.

[ Edit : That is not entirely right. I should have said, 'for variables to be shared among objects, they have to be static'. Static variables are commonly referred to as class variables. And non static variable are referred to as instance variables. So technically, instance variables are not shared among different objects of a type.

I rebooted my laptop before Henry could correct me and take the cow back. Just kidding. I love the corrections. ]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:
Thank you Heena. I think this is where my understanding is slightly flawed. I do understand that there are different copies of "value" for b1 and b2. But similarly, I thought there is one copy of "value" in the Super class which has been modified first to 20 and then to 40.


Question. Do you know the difference between a static variable and an instance variable?

[EDIT: that is the second time that you beaten me to the answer -- you earned a cow ... ]

Henry
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I love the cows. Thank you.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Heena and Henry. Yes, I think I know the difference between static and instance variables. Static variables are shared among the objects whereas instance variables are kept separately for each object. I think part of my problem is this: Are 2 "Base" objects created when this program finishes?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:I think part of my problem is this: Are 2 "Base" objects created when this program finishes?

I have a feeling you're thinking of objects the way that C++ does - that the superclass is part of the memory map of the subclass. That's not the case in Java (or at least, if it is, we don't know about it).

The fact is that your program created two objects: a Derived and a DerivedTwo, and since both of those objects ARE Base's, the answer to your question is 'yes'.

Winston
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Winston. I think I didn't make my question clear. I wanted to ask if calling Derived() will create 2 objects - one for Derived and one for Derived's super, which is Base because Base's constructor also runs.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Thanks, Winston. I think I didn't make my question clear. I wanted to ask if calling Derived() will create 2 objects - one for Derived and one for Derived's super, which is Base because Base's constructor also runs.

No. That's C++ thinking. If that was the case, then you'd get 3 objects wouldn't you? - because ALL classes extend Object.

Winston
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Winston. To confirm my understanding, acessing "super.value" or just "value" from class Derived will access the same variable in the same memory location?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Thank you, Winston. To confirm my understanding, acessing "super.value" or just "value" from class Derived will access the same variable in the same memory location?


Correct. Since the Derived class doesn't declare the value instance variable. The instance methods of the Derived class (that doesn't declare a local variable name value) can access the instance variable via value or super.value (although, it is better to just use value).

Henry
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!