How does "polymorphism" applies to members?
Hello!I also got this question at a jobb interview, se code below.
I said that the output would be x.i = 10 since when polymorphism is used; it is the class of the object that matters and not the reference during execution.
But when I execute it it prints out: x.i = 5
class X
{
public int i = 5;
}
class Y extends X
{
public int i = 10;
}
public class Test1
{
public static void main( String[] args )
{
X x = new Y();
System.out.println( "x.i = " + x.i );
}
}
To me this is stange, too strange since I thougt I know this.
A bit embarrassing!
So if any one could explain how this works please let me know!
Best regards
Fredrik
