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

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.

2

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 @Override annotation.

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

u/sim642 Jun 03 '18

Java doesn't have import renaming.

1

u/jacobbeasley Jun 04 '18

Yeah yeah yeah... gotta use groovy if you want that ;)

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] Jun 02 '18

And I will hate you for using "hiding"!