//Java 语言允许你隐藏变量、嵌套类型,甚至静态方法。///当您声明一个域、一个静态方法或一个嵌套类型时,如果其名称与基本类别//中相应的可访问域、方法或类型相同,则隐藏。///一旦基类的一种方法或域被隐藏在子类中,你就不能在子类的例子中调用它(除了在子类中,使用super 关键字)。///如果一个类需要隐藏一个域,而用于隐藏该域的域的可访问性比隐藏域更有限,//那么这就违反了包容性(subsumption)原则,即著名的Liskov 置换原则(Liskov Substitution Principle)//这一原则指出,你可以为基类做任何事情,它们也可以作用于子类。///不能用实例方法覆盖基类的static方法。///不能用实例方法覆盖基类的static方法。///不能用static方法隐藏基类的实例方法。////不能使用static或实例方法 隐藏和覆盖 任何final类型的基类方法。//覆盖和隐藏的行为是指子类中可见的基类方法。如果基类中的方法使用private修饰符,则可以随意在子类中使用相同的签名方法。//final 对于方法和域来说,修饰符意味着一些完全不同的事情。对于方法,final 这意味着该方法不能复制//(对于实例方法)或隐藏(对于静态方法)。对于域,final 这意味着该域不能被赋值超过一次。//关键词相同,但其行为完全无关。//编译错误:Base.name是隐藏的,所以Derived是访问的.name,而Derived.private是name,在类外是看不见的。//System.out.println(new Derived().name);System.out.println(((Base) (new Derived())).name); //Base nameSystem.out.println(Derived.msg); //Derived msgBase base = new Derived();base.show(); //Derived.show(): Derived namebase.show1(); //Base.show1(): Base strbase.show2(); //Derived.show2(): Derived infobase.print(); //Base.print(): Base msg Derived derived = new Derived();derived.show1(); //Base.show1(): Base strderived.show2(); //Derived.show2(): Derived infoderived.print(); //Derived.print(): Derived msgclass Base {public String name = "Base name";public String str = "Base str";public static String msg = "Base msg";public static String info = "Base info";public void show() {System.out.println("Base.show(): " + name);}public void show1() {System.out.println("Base.show1(): " + str);}public void show2() {System.out.println("Base.show2(): " + info);}public static void print() {System.out.println("Base.print(): " + msg);}public final void print1() {System.out.println("Base.print1(): ");}public final static void print2() {System.out.println("Base.print2(): ");}}class Derived extends Base {private String name = "Derived name";public String str = "Derived str";public static String msg = "Derived msg";public static String info = "Derived info";/* 编译错误:静态方法不能隐藏基类的实例方法public static void show() {System.out.println("Derived.show()");}*/public void show() {System.out.println("Derived.show(): " + name);}public void show2() {System.out.println("Derived.show2(): " + info);}/* 编译错误:实例方法不能覆盖基本静态方法 public void print() {System.out.println("Derived.print()");}*/public static void print() {System.out.println("Derived.print(): " + msg);}/* 编译错误:不能隐藏或覆盖基类的final方法,无论是static还是实例方法。public void print1() {System.out.println("Derived.print1(): ");}public static void print1() {System.out.println("Derived.print2(): ");}public static void print2() {System.out.println("Derived.print2(): ");}*/}