1.子类不能继承父类的私有方法
package com.dbzhang.order; public class C { private void f(){ System.out.println("C类的私有方法f()"); } /** * 主要验证子类是不能继承父类中private方法的 */ public static void main(String[] args) { C c = new D(); c.f(); } }
package com.dbzhang.order; public class D extends C{ public void f(){ System.out.println("D的public方法"); } }
输出结果:
C类的私有方法f()
汇总:
(1)子类不能继承父类中的私有方法,即不能重载父类中的private的方法;
(2)如果某个方法是私有的则不具有多态型;
2.静态的方法不具有多态性:
package com.dbzhang.order; /** * 这个方法主要是用来验证static的方法不具有多态性 * @author Administrator * */ public class C { public static String f(){ return "2"; } public String getAttr(){ return "3"; } }
package com.dbzhang.order; public class D extends C{ public static String f(){ return "1"; } public String getAttr(){ return "4"; } }
package com.dbzhang.order; public class Test { /** * @param args */ public static void main(String[] args) { C c = new D(); System.out.println(c.f()); System.out.println(c.getAttr()); } }
输出结果:
2 4
汇总:
(1)静态的方法不具有多态性,子类不能重写父类中的静态方法;