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

Difference between Hiding, Shadowing & Obscuring

 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain how exactly Hiding, Shadowing & Obscuring are different?
I used to think they are all the same.
TIA
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's lots of good info on this in the JLS, but let me try to explain best I can.
Hiding deals with inheritance of classes. A variable or method in a subclass can hide a variable or method in the superclass. Here's an example:

Shadowing is really the same notion and is, in my opinion, only subtly different. With shadowing, inheritance is not involved. Rather, some variable, which is currently in scope, is "shadowed" by another variable with the same name. This shadowing causes the original variable to be inaccessible (by a simple name) until the new variable goes out of scope. Here's another example:

Obscuring isn't seen very often. Obscuring really occurs when no differentation can be made between the name of a variable, a type, or a package. In general, if you follow proper naming conventions, you'll rarely run into such a case.
Here are some points of interest in the JLS that will give you more info on these three things:
§6.3.1 Shadowing Declarations
§6.3.2 Obscured Declarations
§8.3 Field Declarations
§8.4.6.2 Hiding (by Class Methods)
I hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic