Java Basics--Override and Overload

Table of contents

Override

Rules for overriding:

Overload

Overload rules:

sample code

Difference Between Overriding and Overloading

Summarize 


In Java, rewriting (Override) and overloading (Overload) are two different concepts, used to achieve polymorphism and flexibly handle different method calls.

Override

Override (Override) refers to the subclass redefines the methods that already exist in the parent class. Overriding requires the subclass method to have the same function name, return type, and parameter list as the superclass method, but can have a different method implementation. At runtime, if the method overridden by the subclass is called through the parent class reference, the method implementation in the subclass will actually be executed. Overriding allows subclasses to extend or modify the behavior of methods inherited from parent classes.

Rules for overriding:

  1. The parameter list must be identical to the parameter list of the overridden method.
  2. The return type can be a subclass of the return type of the overridden method (consistent return types are required in Java 5 and earlier, inconsistencies are allowed in Java 7 and later).
  3. Access rights cannot be lower than those of the overridden method. If the method of the parent class is public, the overridden method of the subclass is at least public or protected. If the parent class method is protected, the subclass override method cannot be private.
  4. Only parent class member methods can be overridden. static methods and final methods cannot be overridden. However, methods can be declared static or final again.
  5. If the subclass and the parent class are in the same package, the subclass can override all the methods of the parent class, except the methods declared as private and final.
  6. If the subclass and the parent class are not in the same package, the subclass can only override the non-final methods declared as public and protected by the parent class.
  7. The overridden method may throw any non-mandatory exception, but must not throw any wider mandatory exception than declared by the overridden method.
  8. Constructors cannot be overridden.

It should be noted that if a class cannot be inherited (such as declared as final or located in an inaccessible context), then its methods cannot be overridden.

When the subclass overrides the method of the parent class, the subclass object will not be able to access the overridden method of the parent class. In order to solve this problem, Java provides the super keyword, which can call the normal method of the parent class in the subclass properties, methods, and constructors.

Overload

Overloading (Overload) refers to the definition of multiple methods with the same function name but different parameter lists in the same class. Overloaded methods can have different return types, but the return type alone cannot be used to distinguish overloaded methods. At compile time, the compiler determines which overloaded method to call based on the type and number of parameters when the method is called.

Overload rules:

  1. The overloaded method must change the parameter list, that is, the number or type of parameters is different.
  2. The overloaded method can change the return type, but the return type cannot be used as the only distinguishing criterion. Overloaded methods require a distinct distinction in the parameter list.
  3. The overloaded method can change the access modifier, which can be public, protected, or the default access (no modifier).
  4. Overloaded methods can declare new or wider checked exceptions, that can throw new or subtype exceptions thrown by the parent class.
  5. Overloaded methods can be defined in the same class or in subclasses.

It should be noted that the return value type cannot be used as a criterion for distinguishing overloaded functions, because the compiler only determines which overloaded method to call based on the parameter list when calling.

sample code

Here is a sample code about rewriting and overloading:

// 父类 Animal
class Animal {
    public void makeSound() {
        System.out.println("动物发出声音");
    }
}

// 子类 Dog 继承自父类 Animal
class Dog extends Animal {
    // 方法重写
    @Override
    public void makeSound() {
        System.out.println("狗在汪汪叫");
    }

    // 方法重载
    public void makeSound(int times) {
        for (int i = 0; i < times; i++) {
            System.out.println("狗在汪汪叫,第 " + (i + 1) + " 声");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.makeSound(); // 输出:动物发出声音

        Dog dog = new Dog();
        dog.makeSound(); // 输出:狗在汪汪叫

        dog.makeSound(3);
        /*
        输出:
        狗在汪汪叫,第 1 声
        狗在汪汪叫,第 2 声
        狗在汪汪叫,第 3 声
        */
    }
}

Difference Between Overriding and Overloading

point of difference method overloading method override
parameter list must be modified must not be modified
return type can be modified must not be modified
abnormal can be modified Can be reduced or removed, must not throw new or broader exceptions
access can be modified Must not be more restrictive (restrictions can be lowered)

Summarize 

Method overriding and overloading are different manifestations of polymorphism in Java. Overloading is a situation in which multiple methods with the same name are defined in a class, but the number, type or order of parameters are different, while overriding means that a subclass has a method with the same name as the parent class method, the same number of parameters, types and return values.

  1. Method overloading is a kind of static polymorphism, which mainly determines which overloaded method to call according to the parameters of the method at compile time. Method overloading allows multiple methods with the same name but different parameters to be defined in the same class, providing a more flexible and convenient way to call methods.
  2. Method rewriting is a kind of dynamic polymorphism. Through the relationship between the parent class and the subclass, the subclass can rewrite the method inherited from the parent class to realize the polymorphism of the method. At runtime, it decides which overridden method to call according to the actual type of the object, which has more flexible and diverse behaviors.

In summary, method overloading is static and is distinguished by parameters; method rewriting is dynamic and is distinguished by inheritance relationship and actual type at runtime. Together, they represent the polymorphism mechanism in the Java language.

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/132314301