Guys, My question is very simple. Look at the following code:
public class Test {
public static void main(String[] args){
SubTest st = new SubTest();
st.sayName();
}
}
class BaseTest{
String name= "BaseTest";
void sayName(){
System.out.println(getClass());
System.out.println(this.name);
}
}
class SubTest extends BaseTest{
String name= "SubTest";
}
Output:
xxx.xxx.SubTest
BaseTest
I know the methods will be inherited or overridden and the fields will be shadowed in the inheritance. So it is supposed that the value of subclass is printed, not that of baseclass. However, there must be something I dropped. Hope someone can tell me. Thanks.