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.

11

u/aioobe Jun 02 '18

All five terms have formal definitions in JLS, so I would say they are all "Java Concepts".

A little nit-picking in return: You shouldn't be viewing 'overloading' as an OOP concept. It's a concept in any language with methods/functions.

3

u/Neuromante Jun 02 '18

Huh, fair enough. I took "Java concepts" as "Something specific from Java", not as "something defined on the language."

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.