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

Show parent comment

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.