This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Shadow variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Shadow variables" Watch "Shadow variables" New topic
Author

Shadow variables

Sharan Ashok Vasandani
Greenhorn

Joined: Jul 15, 2010
Posts: 6
Hi,

Is shadowing of variables from Super Class in Sub class a good practice?
Is there a situation where it is a must and cannot be avoided?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Sharan Ashok Vasandani wrote:Is shadowing of variables from Super Class in Sub class a good practice?

It adds possible ambiguity. I'd prevent it if possible - and as far as I know it's always possible; just rename the variable.

Is there a situation where it is a must and cannot be avoided?

Since there is virtually no limit on the allowed variable names it's almost always possible to avoid it, just by renaming it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sharan Ashok Vasandani
Greenhorn

Joined: Jul 15, 2010
Posts: 6
is variable shadowing a best practice or not?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

If I say I'd prevent it if possible, does that sound like it's a good thing?
 
GeeCON Prague 2014
 
subject: Shadow variables