• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

polymorphic variables

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the following code returns 5,sub?

class Super {
int index = 5;
public void printVal()
{
System.out.println( "Super" );
}
}
class Sub extends Super{
int index = 2;
public void printVal() {
System.out.println( "Sub" );
}
}
public class Runner {
public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}

Thanks,
Vikram
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer - Unlike methods, variables are not polymorphic. They are resolved at compile time.
Does this ring any bells??
Ajith
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikram
The printVal method is first defined in Super and is overridden in Sub. When you call this method on an object of class Sub, the overriding method is invoked.
The variable index first defined in Super is shadowed in Sub. The value printed depends on the type of the reference variable used. In your case sup is of type Super. Hence, 5 is printed.
Good question. This is an important concept in OO.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood how sub is printed, but didn't understand how 5 is
printed because sup object's class is Sub and when sup.index
is called it should go and find index in class Sub and
should print 2, why is it printing 5
can any body explain this clearly..,
thanks in advance
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhasker,
I think it is this way...
Super sup = new Sub() means , the reference variable sup is of type Super, and its class is Sub.
Since it is of type Super, the value of variable index in class Super is printed,(which is 5) and since the class of sup is Sub, the method printVal() of class Sub is overridden..(which prints out Sub)...
I myself am quite a beginner, Ajith, do correct me if I am wrong...
thanks,
vidya.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
critical text- Super sup = new Sub();
"when a method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of reference that determines which method implementation will be executed."
in above line, class of current object is Sub, whereas type of current object is Super
1. invocation sup.index depends on type of current object ie. Super.
output = 5
2. invocation sup.printval() depends on class of current object ie. Sub
output = Sub
also refer pgs. 179-181 Khalid
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Vidya is right.
When you have a reference type, the method call is resolved using what the reference is pointing to, than the type of the reference itself. This, we all know, is commonly referred to as Polymorphism. That is what is happening in the example. Though sup is of type Super, it is pointing to an instance of a Sub classtype. Hence the method on the Sub class type is invoked at runtime.
However, for variables and private methods, there is no polymorphism. The symbols and references are resolved at compile time using the type of the reference rather than the type the reference is pointing to. Though sup is pointing to an instance of Sub, sup is declared as type Super. Hence the variable index in super is printed.

Hope this helps( better than my one line answer )
Ajith
[This message has been edited by Ajith Kallambella (edited August 10, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic