• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheriting Variables  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand the following code:
Of course I understand class A and its variable i. But what's about class B and with the variable i in general?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what problems you are having with the code?
Are there compiler errors?
Does the code do what is expected when executed?
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is about hiding variables. I think private int i = 30 hides public int i = 10. Is that correct? Maybe I want to use public int i = 10 from class A in class B. How can I differentiate between public int i = 10 and private int i = 30?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that correct?

Write some testing code that prints the variable, compile and execute it and see what is printed.

Maybe I want to use public int i = 10 from class A in class B

You never want to do that.  It makes for confusion.  There should be unique names.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing some testing code, compiling and running may give me a result but no explanation. I am looking for a proper explanation.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, there are some mods here that love to write tutorials about why things work the way they do.  Let's wait until one of them shows up.
I stick mostly with working to get code to compile and execute.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, that is a simple question. I think it is just hiding. Am I right?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you compiled and executed the testing program?  Can you post the code that goes with your question?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:Please, that is a simple question. I think it is just hiding. Am I right?


Yes.

(Hopefully you can classify that answer as a "proper explanation".)

(And by the way "it's a simple question" is a fallacy. It leads people to believe that simple questions must have simple answers, which is not necessarily the case.)
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just compiled the following code:
The output was: 30
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you need to test about "inheriting variables" is this:



Here the compiler knows that the type of test is A, and since variables aren't inherited, it should at run time use A's variable. Whose value is 10.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!