Overriding is a very popular concept in programming languages. Method overriding in Java is the case where we have the same method present in the superclass as well as the subclass. It’s one of the OOPS Concepts to implement runtime polymorphism in Java.
覆盖是编程语言中非常流行的概念。 在Java中重写方法的情况是,我们在父类和子类中都存在相同的方法。 它是在Java中实现运行时多态性的OOPS概念之一。
用Java覆盖 (Overriding in Java)
Let’s see how we normally override a method in java.
让我们看看我们通常如何重写Java中的方法。
package com.journaldev.annotations;
public class BaseClass {
public void doSomething(String str){
System.out.println("Base impl:"+str);
}
}
Now we will create a subclass overriding BaseClass doSomething
method.
现在,我们将创建一个重写BaseClass doSomething
方法的子类。
package com.journaldev.annotations;
public class ChildClass extends BaseClass{
public void doSomething(String str){
System.out.println("Child impl:"+str);
}
}
Now let’s create a test class to check how overriding works in java.
现在,让我们创建一个测试类来检查重写在Java中的工作方式。
package com.journaldev.annotations;
public class OverrideTest {
public static void main(String[] args) {
BaseClass bc = new ChildClass();
bc.doSomething("override");
}
}
Output of the above program is:
上面程序的输出是:
Child impl:override

Method Overriding In Java
Java中的方法重写
Java覆盖如何工作? (How Java Overriding Works?)
- The method signature must be exactly the same in the superclass and the subclass. 方法签名在父类和子类中必须完全相同。
- When the instance is created, subclass constructor must be used. 创建实例时,必须使用子类构造函数 。
- At the compile time, the variable refers to the superclass. However, in runtime, it refers to the subclass object. 在编译时,变量引用超类。 但是,在运行时,它引用子类对象。
- When the method is called on the variable, it looks like the superclass method will be called. But the method is present in the subclass object, hence it’s called. 当在变量上调用该方法时,似乎将调用超类方法。 但是该方法存在于子类对象中,因此被调用。
Here “bc” is of type BaseClass but at runtime, it’s the object of ChildClass. So when we invoke its doSomething(String str)
method, it looks for the method in ChildClass and hence the output.
这里的“ bc”是BaseClass类型的,但是在运行时,它是ChildClass的对象。 因此,当我们调用其doSomething(String str)
方法时,它将在ChildClass中查找该方法,并因此在输出中查找。
Java Override是否可以与其他方法签名一起使用? (Will Java Override work with different method signature?)
Now let’s change the BaseClass doSomething method like below.
现在,让我们如下更改BaseClass的doSomething方法。
//Change argument from String to Object
public void doSomething(Object str){
System.out.println("Base impl:"+str);
}
You will notice that compiler won’t throw any warnings/errors and now if you run the test program output will be;
您会注意到编译器不会引发任何警告/错误,现在,如果您运行测试程序,输出将是:
Base impl:override
The reason is that BaseClass doSomething(Object str)
method is not anymore overridden by the ChildClass. Hence it’s invoking BaseClass implementation. ChildClass is overloading the doSomething() method in this case because the method name is the same but the method signature is different.
原因是ChildClass不再重写BaseClass doSomething(Object str)
方法。 因此,它正在调用BaseClass实现。 在这种情况下,ChildClass将重载doSomething()方法,因为方法名称相同,但方法签名不同。
Java @Override批注 (Java @Override Annotation)
Java 1.5 introduced annotations. Java @Override
annotation is one of the default java annotation. When we use this annotation for a method, it tells the compiler that we are trying to override a superclass method.
Java 1.5引入了注释 。 Java @Override
批注是默认的Java批注之一。 当我们将此注释用于方法时,它告诉编译器我们正在尝试覆盖超类方法。
If you uncomment the @Override annotation in ChildClass, you will get following compile-time error message:
如果取消注释ChildClass中的@Override注释,则会收到以下编译时错误消息:
The method doSomething(String) of type ChildClass must override or implement a supertype method
Java @Override注释的好处 (Java @Override annotation benefits)
- Java @Override annotation will make sure any superclass changes in method signature will result in a compile-time error and you will have to do necessary changes to make sure the classes work as expected. Java @Override批注将确保方法签名中的任何超类更改都将导致编译时错误,并且您将必须进行必要的更改以确保类按预期工作。
- It’s better to resolve potential issues at compile time than runtime. So always use java @Override annotation whenever you are trying to override a superclass method. 最好在编译时解决潜在问题,而不要在运行时解决。 因此,在尝试覆盖超类方法时,请始终使用java @Override注解。
覆盖与重载 (Overriding vs Overloading)
Overriding | Overloading |
---|---|
Overriding happens between superclass and subclass | Overloading is present in the same class |
The method signature must be the same in superclass and subclass. | The method name must be the same but method signature must be different. |
Identification of the method to be called happens at runtime. | We can identify the method to be called at compile-time. |
If overriding breaks, it can have adverse effects because the program will compile and run. However, the method from superclass will be called rather than subclass. | If we change method name to break overloading, the error will come at compile time. So it’s easy to fix and don’t cause much harm. |
Overriding is also called Runtime Polymorphism. | Overloading is called as Compile-time Polymorphism. |
覆写 | 超载 |
---|---|
重写发生在超类和子类之间 | 重载存在于同一类中 |
方法签名在超类和子类中必须相同。 | 方法名称必须相同,但方法签名必须不同。 |
在运行时识别要调用的方法。 | 我们可以确定在编译时要调用的方法。 |
如果覆盖中断,则可能会产生不利影响,因为该程序将编译并运行。 但是,将调用超类中的方法,而不是子类。 | 如果我们更改方法名称以打破重载,则错误将在编译时出现。 因此很容易修复,不会造成太大伤害。 |
覆盖也称为运行时多态。 | 重载称为编译时多态。 |
摘要 (Summary)
Overriding in Java is very popular and you can find many examples in JDK itself. It’s mostly present where we have a base class with default implementation of the method and then the subclass overriding it and implementing it.
Java重写非常流行,您可以在JDK本身中找到许多示例。 它主要存在于我们拥有一个基类的情况下,该基类具有该方法的默认实现,然后是子类覆盖它并实现它。
Reference: Oracle Documentation
参考: Oracle文档
翻译自: https://www.journaldev.com/817/java-override-method-overriding