Home > Java > JavaBase > body text

The difference between method overloading and overriding in java

王林
Release: 2019-11-12 14:50:25
Original
5570 people have browsed it

The difference between method overloading and overriding in java

Method overloading in Java occurs when two or more methods in the same class have the same method name but different parameters. In contrast, method overriding means that the subclass redefines the method of the parent class. Method overrides must have the same method name, parameter list and return type.

Overloading

(1) Method overloading is a means for a class to process different types of data in a unified way. Multiple functions with the same name exist at the same time, with different number of parameters (types). Overriding is a manifestation of polymorphism in a class.

(2) Java method overloading means that you can create multiple methods in a class. They have the same name but different parameters and different definitions. When calling methods, you decide which method to use by passing them different number of parameters and parameter types. This is polymorphism.

(3) When overloading, the method name must be the same, but the parameter type and number are different, and the return value type can be the same or different. The return type cannot be used as a criterion for distinguishing overloaded functions.

Rewrite (overwrite)

(1) Polymorphism of parent class and subclass, redefine the functions of the parent class. If a method defined in a subclass has the same name and parameters as its parent class, we say the method is overridden. In Java, subclasses can inherit the methods of the parent class, and there is no need to rewrite the same methods. But sometimes the subclass does not want to inherit the methods of the parent class unchanged, but wants to make certain modifications, so method rewriting is used. Method overriding is also called method overwriting.

(2) If a method in the subclass has the same method name, return type, and parameter list as a method in the parent class, the new method overwrites the original method. If you need the original method of the parent class, you can use the super keyword, which refers to the parent class of the rent class.

(3) The access rights of subclass functions are greater than those of the parent class.

Recommended tutorial: Java tutorial

The above is the detailed content of The difference between method overloading and overriding in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template