This article has included “Java often meet questions” : gitee.com/mydb/interv…

In object-oriented programming, method override is a language feature, which is a concrete manifestation of polymorphism. It allows a subclass to redefine existing methods in the parent class, and the method name, parameter type and number in the subclass must be the same as the parent class. This is method override.

The simplest example of method overwriting is as follows: define a Father class and a Son class with a method method in the parent class, and override method in the child class. The parent class Father implements the following code:

/** ** parent */
class Father {
    public void method(String name) {
        System.out.println("Father:"+ name); }}Copy the code

Subclass to rewrite the parent class method method, the specific implementation code is as follows:

/** * subclass */
class Son extends Father {
    @Override
    public void method(String name) {
        // The subclass redefines the behavior of printing in Father:XXX instead of Son:XXX
        System.out.println("Son:"+ name); }}Copy the code

Call and execute method method in the program, the specific implementation code is as follows:

public class OverrideExample {
    public static void main(String[] args) {
        Father father = new Son();
        father.method("Java"); }}Copy the code

The execution result of the above program is as follows:However, in the process of method rewriting, the following issues need to be paid attention to.

Note 1: The subclass permission controller cannot be smaller

In Java, permission control characters have the following levels:

Public > protected > None > private

If the methods in the parent class define protected controls, the code is as follows:

class Father {
    protected void method(String name) {
        System.out.println("Father:"+ name); }}Copy the code

If a subclass overrides its parent method with a permission control less than protected, it will get an error, as shown below:So the question is, can access controls in subclasses be larger? The answer is yes, as the chart below shows:Conclusion: When a subclass overrides a parent class’s method, the overridden method permission control cannot be smaller. It can be equal to or greater than the parent class’s permission control.

Note 2: The return value type of a subclass can only be smaller

In Java, Number is the parent of Long, as shown in the figure below.Next, we use the Number type in the parent class to indicate the return type of the method:

class Father {
    public Number method(int num1, int num2) {
        returnnum1 + num2; }}Copy the code

Long, a subclass of Number, can override the methods of the parent class, as shown in the following figure:Of course, it is also possible if the return type is the same as that of the parent class, as shown below:However, an error is reported if you try to make the return type in a subclass larger, as shown below (Object is the parent of Number) :

Note 3: The type of exception thrown must be small

If the type of exception thrown in the subclass becomes larger, that is, the type of exception thrown in the subclass method is larger than the type of exception thrown in the parent class method, then the program will report an error, as shown in the following figure:The correct solution is to keep the types of exceptions thrown by the parent and child classes the same, as shown below:

Note 4: The method names must be the same

If a subclass overrides a method whose name is not the same as the parent class, the program will also report an error, as shown in the following figure:

Note 5: The type and number of method parameters must be the same

The type and number of method parameters in a subclass must be the same as that of the parent class method; otherwise, an error will be reported, as shown in the following figure.

Method parameter types are inconsistent

Method has an inconsistent number of parameters

conclusion

This article introduced method Override in Java, the process of redefining an existing method in a subclass of a parent class, as a concrete manifestation of polymorphism in object-oriented programming. We can Override a method in a parent class using the @override keyword, but there are five things to be aware of when doing so:

  1. Subclass methods cannot have smaller permission controls;
  2. Subclass methods can only return smaller types;
  3. Subclasses can only throw exceptions of a smaller type;
  4. The subclass method name must be the same as the parent class.
  5. Subclass methods must have the same type and number of arguments as their parent classes.

Reference: Code Out Efficiently

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public account: Java Chinese Community