my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable Shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable Shadowing" Watch "Variable Shadowing" New topic
Author

Variable Shadowing

raj sekar
Ranch Hand

Joined: Mar 11, 2002
Posts: 79
Hi Everyone,
Need some help on Variable Shadowing.
"Variables are shadowed not overrriden" :roll:
Thanks,
Raj


Thanks,
RajN - (SCJP,SCWCD,SCBCD,SCSA,IBM XML 142 )
http://www.geocities.com/n_rajasekar/index.html
You do what you love, love what you do and always deliver more than you promise
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
rajn,
Welcome to Javaranch
I'd like you to read the Javaranch Naming Policy and change your publicly displayed name. Thank you.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Variables can be shadowed or hidden but not overriden.
Please read JLS sections 6.3.1, 8.3 , 8.3.3.1 , 8.3.3.2 and 8.4.8.4
Basically in an field access expression the field accessed is always in the declared type of the reference in the expression:
Type variable = new SubType();
System.out.println(variable.field);
will print the field in the class Type. However:
System.out.println(variable.method());
will invoke the method defined in SubType if it happens to be a polymorphic one. That is, methods can be overriden because the JVM finds out the runtime type of the object pointed by variable when a polymorphic method is invoked on it, but not when a field is accessed on it.


SCJP2. Please Indent your code using UBB Code
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Ok, who can explain (clearly) the differences between hiding, shadowing, and obscuring. All three are referenced in the JLS. But the majority of the time, we talk about static fields and methods hiding the versions with the same signature in the superclass.
So what does it mean to "shadow" or "obscure" something, and how is this different than hiding?


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
From JLS 6.3.2 Obscured Declarations

Obscuring is distinct from shadowing (�6.3.1) and hiding (�8.3, �8.4.6.2, �8.5, �9.3, �9.5). The naming conventions of �6.8 help reduce obscuring.

A good way to figure this out is to go through the JLS index and search for Shadowing, Hiding and Obscuring. That way you get all possible references within the JLS where those terms occur and their exact meaning for each specific cases.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
JLS 6.3.2

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type or a package. In these situations, the rules of �6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

JLS 6.3.1

Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.
...
Hiding, in the technical sense defined in this specification, applies only to members which would otherwise be inherited but are not because of a declaration in a subclass.
raj sekar
Ranch Hand

Joined: Mar 11, 2002
Posts: 79
Want to share some more details on Variable shadowing.
Methods are overridden and Variables are shadowed.

The famous Car/SportsCar Question.
class Car
{
int gearRatio = 10;
String method() { return "From class Car "; }
}
class SportsCar extends Car
{
int gearRatio = 5;
String method(){ return "From class SportsCar"; }
public static void main(String[] args)
{
Car car = new SportsCar();
System.out.println("GearRatio = " + car.gearRatio + " " + car.method());
}
}

Why does it print 'GearRatio = 10 From class SportsCar' although I have overridden the variable gearRatio in SportsCar ???
The concept involved here is ' variables are shadowed and methods are overridden'.
This means that the variable to be selected depends on the declared class of the variable and method to be selected depends on the actual object's class the variable is refering to. In the above question, the declared class of variable 'car' is Car, so gearRatio of class Car is selected but as the class of actual object pointed to by variable 'car' is SportsCar, method() of SportCar is selected.
Consider this :
SportsCar car = new SportsCar();
System.out.println(car.gearRatio); //This will print 5, ie. from SportsCar.
System.out.println(car.method() ); //This will print 'From SportsCar, ie.again from SportsCar.
Now,
Car justCar = (Car) car;
System.out.println(justCar.gearRatio); //This will print 10, ie. from Car as the variable justCar is of class Car.
//So, here you are actually going behind the class SportsCar (By casting) and then accessing the gearRatio. So we say that the variable gearRatio of Car is shadowed by gearRatio of SportsCar.

System.out.println(justCar.method() ); //This will print 'From SportsCar, ie.still from SportsCar. You cannot go behind even by casting, so we say that method() of Car is overridden by method() of SportsCar.
Raj
Jeff Allen
Greenhorn

Joined: Feb 09, 2002
Posts: 20
Thanks Raj, for simplifying what was a difficult concept for me to grasp before.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Also, wait for the May newsletter which will include a nice article by Cindy Glass about shadowing, obscuring, and hiding...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable Shadowing