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
------------------