Your class gave me compile error:
EPractice$1$5.java:13: getObject() in SubClass cannot override getObject() in EP
ractice$1$5; attempting to use incompatible return type
found : B
required: A
public B getObject(){return new B();}
^
1 error
......................................................
After change the return type to A, and add two override functions
class A
{
int x=5;
public void foo(){
System.out.println("A foo");
}
}
class B extends A
{
int x=6;
public void foo(){
System.out.println("B foo");
}
}
class SubClass extends EPractice$1$5
{
public A getObject(){return new B();}
}
public class EPractice$1$5
{
public A getObject(){return new A();}
public static void main(
String[] args)
{
EPractice$1$5 e=new SubClass();
e.getObject().foo();
A a = new B();
System.out.println("x1="+a.x);
B b = new B();
System.out.println("x2="+b.x);
}
}
And here is the output
java EPractice$1$5
B foo
x1=5
x2=6
so I guess for method override, it will call the real object instance's method, but for instance variable, it's different story.