r/java • • Jun 02 '18

5 Java concepts explained: Overloading, overriding, shadowing, hiding, and obscuring

https://programming.guide/java/overloading-overriding-shadowing-hiding-obscuring.html
240 Upvotes

23 comments sorted by

View all comments

13

u/Neuromante Jun 02 '18

Can "shadowing", "hiding" and "Obscuring" be considered "java concepts" or just bad/weird coding practices?

In fact, can also Overloading and Overriding be considered "Java Concepts"? Because I see them as basic OOP concepts.

6

u/lpreams Jun 02 '18

Obscuring is definitely bad practice, but I'd argue shadowing and hiding aren't necessarily.

For example, the author mentions that constructor params often shadow fields. I think this helps make it obvious that the constructor is just setting those fields directly, not computing them.

Hiding is useful when a superclass uses a defaultish value but the subclass needs something different (the author's example of Animal.legs=2 and Dog.legs=4 isn't great, but acceptable). Hiding is for fields what overriding is for classes.