1.不能“覆盖”私有方法
package chapter8; /**
* 不能"覆盖"私有方法
*/
public class PrivateOverride {
private void f() {
System.out.println("private f()");
} public static void main(String[] args) {
PrivateOverride po=new Derived();
po.f();
Derived d=new Derived();
d.f();
}
} class Derived extends PrivateOverride {
public void f() {
System.out.println("public f()");
}
}
【运行结果】:
private f()
public f()
2.域不呈多态性(只有普通方法具有多态)
/**
*
*/
package chapter8; /**
* 缺陷:域与静态方法
*/
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();
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
3静态方法不呈多态性
package chapter8; /**
* 静态方法不具有多态性
*/
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();
System.out.println(sup.StaticGet());
System.out.println(sup.dynamicGet());
}
}
【运行结果】:
Base staticGet()
Derived dynamicGet()