One of the from Mock Exam
class A
{
int x = 5;
}
class B extends A
{
int x = 6;
}
class CoVarTest
{
public A getObject()
{
System.out.println("A");
return new A();
}
public static void main(
String[] args)
{
CoVarTest c1 =new
Test();
System.out.println(c1.getObject().x);
}
}
class Test extends CoVarTest
{
public B getObject()
{
System.out.println("B");
return new B();
}
}
The output is:
B
5
My Doubt is the method c1.getObject() calls the method "public B getObject()", then why the value of x 5 instead of 6?
I know that overriding is not for variables they are choosen at compile time.
Please provide some thought on this......
Thanks in Advance.
Regards,
Varsha