Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

variable shadowing

Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables are resolved at compile time.
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic