Home  >  Article  >  Java  >  Introduce the differences and connections between override and overload

Introduce the differences and connections between override and overload

巴扎黑
巴扎黑Original
2017-07-24 14:01:371742browse

1 Overview

1. What is overloading?

The mechanism of using formal parameters to distinguish multiple methods with the same name in the same class is called overloading.

2. What is rewriting?

The subclass inherits the parent class, and the process of overriding the parent class method is called rewriting.

Second comparison

1. Occurrence scope

Rewriting occurs between the parent class and the child Between classes, involving two classes, overloading occurs within the same class.

2. Constraint

Override

  1. subclass in When overriding a parent class method, the return value must be a subclass of the parent class method's return value.

  2. The access permission of the parent class cannot be reduced, that is, if the access permission of the parent class method is protected, the subclass cannot change it to private when overriding the method.

  3. # The exception thrown must be a subclass of the parent exception.

  4. # Static methods cannot be overridden.

  5. #Final type methods cannot be overridden.

Overloading
Overloading distinguishes methods with the same name based on their formal parameters, so formal parameters between methods with the same name They cannot be exactly the same. The parameter types and numbers are exactly the same, but the order is different, and they belong to different formal parameters.

The above is the detailed content of Introduce the differences and connections between override and overload. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn