覆盖和隐藏方法

原文: https://docs.oracle.com/javase/tutorial/java/IandI/override.html

实例方法

子类中具有相同签名(名称,加上其参数的数量和类型)和返回类型作为超类中的实例方法的实例方法会覆盖超类的方法。

子类覆盖方法的能力允许类从行为“足够接近”的超类继承,然后根据需要修改行为。覆盖方法具有相同的名称,参数的数量和类型,以及返回类型作为它覆盖的方法。重写方法还可以返回由重写方法返回的类型的子类型。该子类型称为协变返回类型

覆盖方法时,您可能希望使用@Override批注指示编译器您要覆盖超类中的方法。如果由于某种原因,编译器检测到该方法在其中一个超类中不存在,那么它将生成错误。有关@Override的更多信息,请参阅 Annotations

静态方法

如果子类定义的静态方法与超类中的静态方法具有相同的签名,则子类中的方法会将中的方法隐藏在超类中。

隐藏静态方法和覆盖实例方法之间的区别具有重要意义:

  • 被调用的重写实例方法的版本是子类中的版本。
  • 被调用的隐藏静态方法的版本取决于它是从超类还是从子类调用的。

考虑一个包含两个类的示例。第一个是Animal,它包含一个实例方法和一个静态方法:

  1. public class Animal {
  2. public static void testClassMethod() {
  3. System.out.println("The static method in Animal");
  4. }
  5. public void testInstanceMethod() {
  6. System.out.println("The instance method in Animal");
  7. }
  8. }

第二个类是Animal的子类,称为Cat

  1. public class Cat extends Animal {
  2. public static void testClassMethod() {
  3. System.out.println("The static method in Cat");
  4. }
  5. public void testInstanceMethod() {
  6. System.out.println("The instance method in Cat");
  7. }
  8. public static void main(String[] args) {
  9. Cat myCat = new Cat();
  10. Animal myAnimal = myCat;
  11. Animal.testClassMethod();
  12. myAnimal.testInstanceMethod();
  13. }
  14. }

Cat类覆盖Animal中的实例方法,并隐藏Animal中的静态方法。此类中的main方法创建Cat的实例并在类上调用testClassMethod()并在实例上调用testInstanceMethod()

该程序的输出如下:

  1. The static method in Animal
  2. The instance method in Cat

正如所承诺的那样,被调用的隐藏静态方法的版本是超类中的版本,被调用的重写实例方法的版本是子类中的版本。

接口中的默认方法抽象方法与实例方法一样继承。但是,当类或接口的超类型提供具有相同签名的多个默认方法时,Java 编译器将遵循继承规则来解决名称冲突。这些规则由以下两个原则驱动:

  • 实例方法优于接口默认方法。

    考虑以下类和接口:

    1. public class Horse {
    2. public String identifyMyself() {
    3. return "I am a horse.";
    4. }
    5. }
    1. public interface Flyer {
    2. default public String identifyMyself() {
    3. return "I am able to fly.";
    4. }
    5. }
    1. public interface Mythical {
    2. default public String identifyMyself() {
    3. return "I am a mythical creature.";
    4. }
    5. }
    1. public class Pegasus extends Horse implements Flyer, Mythical {
    2. public static void main(String... args) {
    3. Pegasus myApp = new Pegasus();
    4. System.out.println(myApp.identifyMyself());
    5. }
    6. }

    方法Pegasus.identifyMyself返回字符串I am a horse.

  • 已忽略其他候选项的方法将被忽略。当超类型共享一个共同的祖先时,就会出现这种情况。

    考虑以下接口和类:

    1. public interface Animal {
    2. default public String identifyMyself() {
    3. return "I am an animal.";
    4. }
    5. }
    1. public interface EggLayer extends Animal {
    2. default public String identifyMyself() {
    3. return "I am able to lay eggs.";
    4. }
    5. }
    1. public interface FireBreather extends Animal { }
    1. public class Dragon implements EggLayer, FireBreather {
    2. public static void main (String... args) {
    3. Dragon myApp = new Dragon();
    4. System.out.println(myApp.identifyMyself());
    5. }
    6. }

    方法Dragon.identifyMyself返回字符串I am able to lay eggs.

如果两个或多个独立定义的缺省方法冲突,或者缺省方法与抽象方法冲突,则 Java 编译器会产生编译器错误。您必须显式覆盖超类型方法。

考虑一下现在可以飞行的计算机控制汽车的例子。您有两个接口(OperateCarFlyCar)为同一方法提供默认实现(startEngine):

  1. public interface OperateCar {
  2. // ...
  3. default public int startEngine(EncryptedKey key) {
  4. // Implementation
  5. }
  6. }
  1. public interface FlyCar {
  2. // ...
  3. default public int startEngine(EncryptedKey key) {
  4. // Implementation
  5. }
  6. }

实现OperateCarFlyCar的类必须覆盖方法startEngine。您可以使用super关键字调用任何默认实现。

  1. public class FlyingCar implements OperateCar, FlyCar {
  2. // ...
  3. public int startEngine(EncryptedKey key) {
  4. FlyCar.super.startEngine(key);
  5. OperateCar.super.startEngine(key);
  6. }
  7. }

super之前的名称(在本例中为FlyCarOperateCar)必须引用定义或继承被调用方法的默认值的直接超接口。这种形式的方法调用不限于区分包含具有相同签名的默认方法的多个已实现接口。您可以使用super关键字在类和接口中调用默认方法。

类中的继承实例方法可以覆盖抽象接口方法。考虑以下接口和类:

  1. public interface Mammal {
  2. String identifyMyself();
  3. }
  1. public class Horse {
  2. public String identifyMyself() {
  3. return "I am a horse.";
  4. }
  5. }
  1. public class Mustang extends Horse implements Mammal {
  2. public static void main(String... args) {
  3. Mustang myApp = new Mustang();
  4. System.out.println(myApp.identifyMyself());
  5. }
  6. }

方法Mustang.identifyMyself返回字符串I am a horse.Mustang从类Horse继承方法identifyMyself,它覆盖接口Mammal中同名的抽象方法。

:接口中的静态方法永远不会被继承。

修饰符

覆盖方法的访问说明符可以允许比重写方法更多但不是更少的访问。例如,超类中的受保护实例方法可以在子类中公开,但不是私有的。

如果尝试将超类中的实例方法更改为子类中的静态方法,则会出现编译时错误,反之亦然。

摘要

下表总结了在定义与超类中的方法具有相同签名的方法时会发生什么。

Defining a Method with the Same Signature as a Superclass’s Method | | 超类实例方法 | 超类静态方法 | | —- | —- | —- | | 子类实例方法 | 覆盖 | 生成编译时错误 | | —- | —- | —- | | 子类静态方法 | 生成编译时错误 | 隐藏 | | —- | —- | —- |


Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.