I am confused with the static binding example below. I reckon that S2.x and S2.y shows static binding as they prints out the fields according to s2's static type. And S2.foo() makes s2call the foo method in the super class, as foo is not over ridden in the subclass.
However with S2.goo(), isn't it supposed to call the goo() method in the Test1 subclass? Like it's polymorphism? How come it's static binding? It looks like S2.goo() calls the Super Class goo()method and prints out =13. Many thanks for your help in advance!
public class SuperClass {
public int x = 10;
static int y = 10;
protected SuperClass() {
x = y++;
}
public int foo() {
return x;
}
public static int goo() {
return y;
}
}
and SubClass
public class Test1 extends SuperClass {
static int x = 15;
static int y = 15;
int x2= 20;
static int y2 = 20;
Test1()
{
x2 = y2++;
}
public int foo2() {
return x2;
}
public static int goo2() {
return y2;
}
public static int goo(){
return y2;
}
public static void main(String[] args) {
SuperClass s1 = new SuperClass();
SuperClass s2 = new Test1();
Test1 t1 = new Test1();
System.out.println("S2.x = " + s2.x);
System.out.println("S2.y = " + s2.y);
System.out.println("S2.foo() = " + s2.foo());
System.out.println("S2.goo() = " + s2.goo());
}
}