原资料来自:http://lovewhzlq.iteye.com/blog/452605
1.
public class PrivateOverride {
private void f() { System.out.println("private f()"); }
public static void main(String[] args) {
PrivateOverride po = new DerivedClass();
po.f();
}
}
class DerivedClass extends PrivateOverride {
public void f() { System.out.println("public f()"); }
}
//输出结果为:private f()
分析:private方法被自动认为是final方法,而且对于子类是屏蔽的,因此,DerivedClass类的f()方法是一个新方法,基类中的f()方法对于子类DerivedClass是不可见的,因此不能被重载,导致调用的是声明基类的f()方法。
2.直接访问某个域,这个访问将在编译期进行解析。
class Super {
public int field = 0;
public int getField() { return field; }
}
class Sub extends Super {
public int field = 1;
public int getField() { return field; }
public int getSuperField() { return super.field; }
}
public class FieldAccess {
public static void main(String[] args) {
Super sup = new Sub(); // Upcast
System.out.println("sup.field = " + sup.field +
", sup.getField() = " + sup.getField());
Sub sub = new Sub();
System.out.println("sub.field = " +
sub.field + ", sub.getField() = " +
sub.getField() +
", sub.getSuperField() = " +
sub.getSuperField());
}
}
/* 输出结果:
sup.field = 0, sup.getField() = 1
sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0
*/
当Sub对象转型为Super引用时,任何域访问操作都将由编译器解析,因此不是多态的。
Sub实际上包含两个称为field的域:它自己的和从Super继承来的。
3.如果某个方法是静态的,它的行为也不具有多态性。
class StaticSuper {
public static String staticGet() {
return "Base staticGet()";
}
public String dynamicGet() {
return "Base dynamicGet()";
}
}
class StaticSub extends StaticSuper {
public static String staticGet() {
return "Derived staticGet()";
}
public String dynamicGet() {
return "Derived dynamicGet()";
}
}
public class StaticPolymorphism {
public static void main(String[] args) {
StaticSuper sup = new StaticSub(); // Upcast
System.out.println(sup.staticGet());
System.out.println(sup.dynamicGet());
}
}
/* 输出结果:
Base staticGet()
Derived dynamicGet()
*/
因为静态方法是和类相关联的,而不是某个对象。
1.
public class PrivateOverride {
private void f() { System.out.println("private f()"); }
public static void main(String[] args) {
PrivateOverride po = new DerivedClass();
po.f();
}
}
class DerivedClass extends PrivateOverride {
public void f() { System.out.println("public f()"); }
}
//输出结果为:private f()
分析:private方法被自动认为是final方法,而且对于子类是屏蔽的,因此,DerivedClass类的f()方法是一个新方法,基类中的f()方法对于子类DerivedClass是不可见的,因此不能被重载,导致调用的是声明基类的f()方法。
2.直接访问某个域,这个访问将在编译期进行解析。
class Super {
public int field = 0;
public int getField() { return field; }
}
class Sub extends Super {
public int field = 1;
public int getField() { return field; }
public int getSuperField() { return super.field; }
}
public class FieldAccess {
public static void main(String[] args) {
Super sup = new Sub(); // Upcast
System.out.println("sup.field = " + sup.field +
", sup.getField() = " + sup.getField());
Sub sub = new Sub();
System.out.println("sub.field = " +
sub.field + ", sub.getField() = " +
sub.getField() +
", sub.getSuperField() = " +
sub.getSuperField());
}
}
/* 输出结果:
sup.field = 0, sup.getField() = 1
sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0
*/
当Sub对象转型为Super引用时,任何域访问操作都将由编译器解析,因此不是多态的。
Sub实际上包含两个称为field的域:它自己的和从Super继承来的。
3.如果某个方法是静态的,它的行为也不具有多态性。
class StaticSuper {
public static String staticGet() {
return "Base staticGet()";
}
public String dynamicGet() {
return "Base dynamicGet()";
}
}
class StaticSub extends StaticSuper {
public static String staticGet() {
return "Derived staticGet()";
}
public String dynamicGet() {
return "Derived dynamicGet()";
}
}
public class StaticPolymorphism {
public static void main(String[] args) {
StaticSuper sup = new StaticSub(); // Upcast
System.out.println(sup.staticGet());
System.out.println(sup.dynamicGet());
}
}
/* 输出结果:
Base staticGet()
Derived dynamicGet()
*/
因为静态方法是和类相关联的,而不是某个对象。