Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

var access

 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know that why the var str has the value "Super" and not "Sub".

output : Super
When the var str is also in test why isn't the test's copy of str is being used in R's showvar.
 
Rajeshwari Natarajan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 reasons...
(1) In test, str is a local variable inside main method
(2) showvar() is not overriden by test.
If u want "Sub" to be printed, then this should be the code.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Anupam
In this case, the variable str in main method can not access to the class R, because the str in main method belong to main method only.
So, the class R use own variable str as the print result when invoked the default constructor super() in the constructor test.
In you want print "Sub"

If it is not clear,please post again.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sub variable is local to main. Unless it is passed as an argument to showvar it is not visible within such method.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well what I think is that code would be replaced by the method that showvar() method. I think I am wrong but can you actually tell me if this happens or not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic