class Test {
public static final String FOO = "foo";
public static void main(String[] args) {
Test b = new Test();
Sub s = new Sub();
System.out.print(Test.FOO); /* Prints foo */
System.out.print(Sub.FOO); /* Prints bar */
System.out.print(b.FOO); /* Prints foo */
System.out.print(s.FOO); /* Prints bar */
System.out.print(((Test)s).FOO); /* Prints foo but Why?*/
} }
class Sub extends Test {public static final String FOO="bar";}
I was reading about Inheritance and i found out that even when you cast a SUB CLASS object to PARENT class and try to call a overridden method using that object (see here), the SUBCLASS method will be called.
But here in my Question why the Parent Class variable value is being printed even when the object TYPE is of Class Sub.?