• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reasons for variable hiding  RSS feed

 
Edoardo Pasca
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo everyone,

this is my first post in the forum. I've come here after buying the OCA (and passing exam!) and OCP books from Jeanne and Scott.

Let's come to the question:

What is the reason why Java performs variable hiding?

Everywhere it is written that it is considered bad practice to use it, and one should really be aware of it. I believe that there are some reasons why the Java designers have chosen (or have been forced) to make variable hiding. As there are, for instance, for visibility or for exceptions in overridden methods.
But in the case of variable hiding II do not seem to see any that is useful!

Thanks

Edoardo
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us where you read that variable hiding is a bad thing. It is a general principle of OO programming that data and implementation details should be hidden inside your object, and only exposed via methods. It is also not something the language enforces; it enables it by providing private access. It is something the programmer should implement.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the way that a static method or a field are hidden by similarly named members of the subclass? I thought at first you meant data hiding. Sorry for my mistake.
If you have a subclass, how are you going to program it any other way? The alternative would be to prohibit members of the same name in subclasses, We cannot tell why that decision was made, over twenty years ago, but maybe the alternatives were considered even worse.
 
Edoardo Pasca
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks for you reply,

I understand that (static) method hiding is due to the fact that the actual method to be called is decided at compile time.

Now, for what regards variables (fields) it is not clear to me the rationale behind variable hiding. That it is considered bad practice, I've read on the OCA study guide and here https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html
Generally speaking, we don't recommend hiding fields as it makes code difficult to read.


In Java polymorphism let you treat an object as its type or as a superclass (or interface). In my mind I had this picture (clearly wrong)  that once you have an object in memory you can see it with different lenses/filters (the type)  that allow you to access some members (methods and variables) and not other. However, with variable hiding, the content of my object is not the same if I treat it as its type or as a superclass: if I define in my subclass a member variable with the same name as in the superclass.

For example, the code here defines a parent and a child class. In the child I redefine the variable "name" but when I use a superclass Animal to treat the object Pet (for which name="my pet") I get the variable for the Superclass!!! I find it personally not intuitive and that is why I was looking for a rational reason why this is!




 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edoardo Pasca wrote: I find it personally not intuitive and that is why I was looking for a rational reason why this is!


First of all, there is a big difference between "personally not intuitive" and "everywhere it is written that it is considered bad practice" ...

... but ... to answer your question, we can only speculate on the reason. In my opinion...

I would think that the Java designers didn't want to step too far from the language that it was somewhat based on. The C++ language didn't have polymorphic instance variables, so perhaps, there is a good reason why. And I would think it would be a bad idea. When I code a get/set of a variable, for a compiled language, I expect it to be mapped to a one cycle memory load/store. I don't expect it to do a lookup to see where it the variable is actually located, and do an indirect memory load/store.

Henry
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your examples shows exactly why variable hiding is a bad practice: because it's easy to get confused as to what the behavior of the surrounding code would be. This is what the authors meant by "it makes your code difficult to read." If you did not have line 10, there would be no doubt as to what would be printed out.  Variables don't participate in polymorphism, only method calls are polymorphic. Therefore, the reference to name on line 5 resolves to the name variable declared on line 3, not the one on line 10. This is called static binding, which happens at compile time.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand why Java was designed this way, you have to be familiar with how compilers work, specifically, how it does syntax and lexical analysis. Without going into too much detail, the rule establishes how a variable token is resolved at compile-time and that has a lot to do with the scope of its visibility within the context that it is used. If there are two same-named tokens in a scope, the compiler needs to be able to deterministically choose one of them to resolve references to. The hiding rule is what it follows to do that. Personally, I think it makes a lot of sense but that's because I have a little experience with compiler design and understand the underlying technical issues involved.
 
Edoardo Pasca
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu,

I believe that makes sense.

Edoardo
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!