Home>Article>Java> How to distinguish between the concepts of rewriting and overloading in Java

How to distinguish between the concepts of rewriting and overloading in Java

王林
王林 forward
2020-07-04 16:48:29 2589browse

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete