wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance, variable scope and shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance, variable scope and shadowing" Watch "Inheritance, variable scope and shadowing" New topic
Author

Inheritance, variable scope and shadowing

Francisco Montes
Ranch Hand

Joined: Sep 30, 2009
Posts: 30
Hi everybody,

I am testing a few concepts about variable scope and accesibility to shadowed variables from subclasses.
My question is, given the following code, is there a way to access the SuperSuperClass´s string from the commented line?

Thank you!



SCJP 1.6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
You don't even have to instantiate the SuperSuperClass. Just do the following -



Regards.
Francisco Montes
Ranch Hand

Joined: Sep 30, 2009
Posts: 30
Thanks a lot seetharaman.

I was looking for a more "direct way"... something like System.out.println(super.super.s); (I know that it doesn´t even compile but i hope you see what i mean).

From your reply i take that there is no way to access property s in SuperSuperClass from where i was trying to do it.

If I do it the way you suggest, I would be creating another instance of an object and I would be accesing a different value of SuperSuperClass s which is not really what i was looking for.

Does it makes sense?
Francisco Montes
Ranch Hand

Joined: Sep 30, 2009
Posts: 30
Spot on Vijay!

Now i know that i need to use casting to reach a shadowed variable at that level.

Thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance, variable scope and shadowing