Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance, variable scope and shadowing

 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't even have to instantiate the SuperSuperClass. Just do the following -



Regards.
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spot on Vijay!

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

Thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic