aspose file tools*
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

mohammed mustafa
Greenhorn

Joined: Jan 11, 2001
Posts: 12
in one of khalids example codes a superclass type reference assigned to a subclass reference
is calling the member variable line# 14
// Exceptions
class InvalidHoursException extends Exception {}
class NegativeHoursException extends InvalidHoursException {}
class ZeroHoursException extends InvalidHoursException {}
class Light {
protected String billType = "Small bill"; // (1)
protected double getBill(int noOfHours)
throws InvalidHoursException { // (2)
double smallAmount = 10.0,
smallBill = smallAmount * noOfHours;
System.out.println(billType + ": " + smallBill);
return smallBill;
}
}
class TubeLight extends Light {
public String billType = "Large bill"; // (3) Shadowing.
public double getBill(final int noOfHours)

throws ZeroHoursException { // (4) Overriding.
double largeAmount = 100.0,

largeBill = largeAmount * noOfHours;

System.out.println(billType + ": " + largeBill);

return largeBill;

}

public double getBill() { // (5)
System.out.println("No bill");

return 0.0;

}
}
public class Client {

public static void main(String args[])

throws InvalidHoursException { // (6)
TubeLight tubeLightRef = new TubeLight(); // (7)
Light lightRef1 = tubeLightRef; // (8)
Light lightRef2 = new Light(); // (9)
// Invoke overridden methods

tubeLightRef.getBill(5); // (10)
lightRef1.getBill(5); // (11)
lightRef2.getBill(5); // (12)
// Access shadowed variables

System.out.println(tubeLightRef.billType); // (13)

System.out.println(lightRef1.billType); // (14)

System.out.println(lightRef2.billType); // (15)
// Invoke overloaded method

tubeLightRef.getBill(); // (16)

}
}
since the subclass member overshadow the superclass members how does the
super class member variable protected String billtype="small bill"
is called
out put is large bill:500.0
large bill:500.0
small bill:50.0
large bill
small bill
small bill
no bill
please explain

------------------
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
Member variables are resolved at compile time.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The "shadowing" that you are describing is only true for methods.
This is because variables are created in memory at the time that they are declared. This is resolved at compile time. Then when you place a subclass in that holder (like putting TubeLight in a Light holder), the object is still actually an instance of the superclass and retain the superclasses variables.
Methods however are invoked at runtime ("late-binding"). Since you have a TubeLight in a Light holder the methods of the subclass shadow the methods of the superclass.
Of course if you REALLY want to understand this (and get a few smiles at the same time) you should read: http://www.javaranch.com/campfire/StoryPoly.jsp


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable shadowing