search
HomeJavaJavaBaseThe difference between method overloading and overriding in java

The difference between method overloading and overriding in java

Nov 12, 2019 pm 02:50 PM
javathe differencecoverOverload

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools