• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Method calls in class hierarchy?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a modified example taken from Professor Khalid's book. The output is:
Large bill
Small bill
Small bill

My question is why Small bill is printed even if the the object to which light1 refers to is of type TubeLight. Shouldn't TubeLight's method be called? Isn't that the basis of polymorphism?

Here is the example the question is about:

class Light {
protected static String billType = "Small bill";

public static void printBillType() {
System.out.println(billType);
}
}
class TubeLight extends Light {
public static String billType = "Large bill";

public static void printBillType() {
System.out.println(billType);
}
}
public class Client {
public static void main(String[] args){
TubeLight tubeLight = new TubeLight();
Light light1 = tubeLight;
Light light2 = new Light();

tubeLight.printBillType();
light1.printBillType(); // Shouldn't this print Large Bill
light2.printBillType();
}
}

/* Below are the classes I have written, and they give me the expected
output: In Class Two
What is the difference then between the above class written by Prof.
and below written by me?
*/
class One
{ public void put()
{ System.out.println("In class One"); }
}

class Two extends One
{ public void put()
{ System.out.println("In class Two"); }

public static void main(String[] args)
{ Two two_type = new Two();
One one_type = two_type;

one_type.put(); // It prints In class Two as expected
}
}
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
printBillType is a static method and all static methods are called from its reference type and not the object that has been instantiated. If you were to remove the static identifier then you would get what you are expecting.
hope that helps
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai Sudarshan....
I went through your program.Acoording to my acessment you have to still get through the topic of Inheritance.
I will give you the explanation for Professor Khalid's program.
You have 2 classes. 1.)Light 2.)TubeLight
TubeLight class extends class Light.Since the variable billType is declared protected it is inherited by the class TubeLight.But the point to be noticed here is in class TubeLight variable billType is redeclared and given the value "Large bill".Therefore the inherited billType variable still exists but is shadowed. Hope you are understanding what I am telling.
Ok.... now the method printBillType() ==> It is also inherited by class TubeLight.
Now coming to the class Client.
In this class an instance of the class TubeLight is created.
A reference variable of type Light (here light1) is declared and to this variable the reference variable tubeLight is assigned. What this means is that light1 now points to the instance of TubeLight. Thought it points to the subclass instance the type of the reference variable is parent type. The parent Reference variable will have no knowledge of what a subclass adds to it.
So when you call the method using the light1 reference though it refers to the subclass TubeLight it still prints the value of the variable it is aware of.(So the value "small Bill" is printed instead of "Large Bill".
Remember If a refernce to a sublass object is assigned to a super class reference variable you will have access only to those parts of the object defined by the superclass.It is the type of the reference variable but not the type of the object that it refers to that determines what members can be accessed.
But when method overriding is present the situation changes.It is the type of the object being referred to by the reference variable not the type of the reference variable that determines which version of the overriden method will be executed.
Hope you understood........
If anyone finds my explanation wrong please excuse me and let me know.
Bye Bye
Roja.Potti.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic