r/java • u/aioobe • Jun 02 '18
5 Java concepts explained: Overloading, overriding, shadowing, hiding, and obscuring
https://programming.guide/java/overloading-overriding-shadowing-hiding-obscuring.html2
u/whackri Jun 03 '18 edited Jun 07 '24
silky retire pie mourn waiting connect languid rain hateful stupendous
This post was mass deleted and anonymized with Redact
1
u/aioobe Jun 03 '18
Your understanding is correct and highlights the importance of consistently using
@Overrideannotation.Note that you can activate warnings/errors for "Missing @Override Annotation" under Settings > Editor > Inspections in IntelliJ (and other IDEs presumably have similar features).
28
u/Hatefiend Jun 02 '18
Wow, I actually had no idea about obscuring.
54
u/sim642 Jun 02 '18
Because it never happens with proper naming where variables start lowercase.
-3
u/jacobbeasley Jun 03 '18
import System as Sys;
8
1
u/ShlimDiggity Jun 02 '18
I just dealt with it in my intro to python last night, lol... Some example I looked up named a methods parameter (str). So I thought you were supposed to put the variable type as the parameter, as opposed to the name. I used int, then within the method tried calling int() to convert from string to int. No go! Obscured!
2
Jun 02 '18
[removed] — view removed comment
2
u/ShlimDiggity Jun 02 '18
Yep, Java for sure won't let you name a variable int
1
Jun 02 '18
[removed] — view removed comment
2
u/kalifornia_love Jun 02 '18
Gotten bit by that bug before...
FYI the “pythonic” way to get around this is to name your variable json_
Or use a better/different variable name.
6
u/lukaseder Jun 02 '18
These are a ton of fun to avoid when writing a code generator. Especially when generating classes that obscure java.lang classes
5
u/brett_riverboat Jun 02 '18
The only terms I've heard of are overloading and overriding. The other ones sound like bad practice to me.
2
u/desrtfx Jun 02 '18
The other ones sound like bad practice to me.
Commonly, the other ones (shadowing, hiding, and obscuring) mostly happen because of accidents.
1
u/knoam Jun 02 '18
Is it technically considered overriding when your class implements an abstract method from an interface or abstract class? I usually don't think of it as overriding, but you use the @Override annotation nonetheless.
5
u/aioobe Jun 02 '18
I think so. JLS Chapter 9 on Interfaces uses the term "override" extensively: https://docs.oracle.com/javase/specs/jls/se10/html/jls-9.html#jls-9.4.1 To mention an example:
[...] So, every implementing class would automatically override an interface's toString default [...]
3
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.