


How to distinguish between the concepts of rewriting and overloading in Java
Overloading
(Recommended learning: java entry program)
Method Overloading is a means for a class to handle different types of data in a unified way. Multiple functions with the same name exist at the same time, with different number/type of parameters. Overloading is a manifestation of polymorphism in a class.
Method overloading in Java means that you can create multiple methods in a class. They have the same name but different parameters and different definitions.
When calling methods, the specific number and parameter types passed to them are used to determine which method to use. This is polymorphism.
When overloading, the method name should be the same, but the parameter type and number are different, and the return value type can be the same or different. The return value type cannot be used as a criterion for distinguishing overloaded functions.
When a parent class method is modified by default, it can only be overridden by its subclasses in the same package. If it is not in the same package, it cannot be overridden.
When a method of a parent class is prototyped, it is not only overridden by its subclasses in the same package, but can also be overridden by subclasses in different packages.
Overloaded rules
must have different parameter lists;
can have different return type, as long as the parameter list is different;
can have different access modifiers;
can throw different exceptions ;
Rules for overriding methods
The parameter list must be exactly the same as the overridden method, otherwise it cannot Call it a rewrite but an overload.
The returned type must always be the same as the return type of the overridden method, otherwise it cannot be called an override but an overload.
The restriction of the access modifier must be greater than the access modifier of the overridden method (public>protected>default>private)
Heavy The written method must not throw a new checked exception or a checked exception that is broader than the overridden method declaration. For example: a method of the parent class declares a checked exception IOException. When overriding this method, you cannot throw Exception. You can only throw exceptions of subclasses of IOException, and you can throw unchecked exceptions.
Characteristics of overloading and rewriting (overwriting)
(Video tutorial recommendation: java video tutorial)
Overload Features
1. When using overload, you can only use different parameter styles. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types in the same method must be different, for example, it can be fun(int, float), but it cannot be fun(int, int ));
2. It cannot be overloaded by access permissions, return types, and thrown exceptions;
3. The exception type and number of methods will not affect overloading;
4. For inheritance, if a method has privileged access in the parent class, it cannot be overloaded in the subclass. If it is defined, it will only define a new method. It will not achieve the effect of overloading.
Override Features
1. The flag of the overridden method must completely match the flag of the overridden method to achieve the override effect;
2. The return value of the overridden method must be consistent with the return value of the overridden method;
3. The exception thrown by the overridden method must be consistent with the exception thrown by the overridden method, or Its subclass;
4. The overridden method cannot be private, otherwise only a new method is defined in its subclass and it is not overwritten.
Summary
overload (overload)
At least one of the parameter type, number, and order is different.
Cannot overload method names that only have different return values.
Exists in parent classes, subclasses, and similar classes.
override (rewrite)
The method name, parameters, and return value are the same.
Subclass methods cannot reduce the access rights of parent class methods.
Subclass methods cannot throw more exceptions than parent class methods (but subclass methods may not throw exceptions).
Exists between parent class and child class.
Methods are defined as final and cannot be overridden.
The above is the detailed content of How to distinguish between the concepts of rewriting and overloading in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.