Home >Java >javaTutorial >Overloading and overriding of JAVA methods

Overloading and overriding of JAVA methods

PHP中文网
PHP中文网Original
2017-06-22 14:25:221391browse

Overloading of methods: (used to implement the same function, but requires different parameters to satisfy different users) in the same class.

means that multiple methods with the same name, the same or different return values, but different parameters (number of parameters or parameter types) can be defined in a class. When calling, use overloading, which is simple and convenient for users to use the API. There is no need to give different names to methods with the same function, which is convenient for users. For users, doing the same thing is like calling the same function. Constructors can also be overloaded. As long as the program defines its own construction method, the system does not provide a default construction method.

Overriding of methods (rewriting): (Subclasses override the methods of parent classes, in different classes)

Overriding of methods (override/ overwrite)

Difference from overloading: Method overloading is to define multiple functions in a class with the same name, the same return type, but different parameters

When overwriting a method: It is best to use Copy this function of the parent class

1. In the subclass, you can rewrite the methods inherited from the base class as needed.

2. The overridden method must have the same method name, parameter list and return type as the overridden method.

3. Overriding methods cannot use more restrictive access rights than the overridden method.

Strict order private>default>protecte>public, this is related to polymorphism. You will understand it much more when you talk about polymorphism. When a certain place is modified by the parent class, a method of the parent class can be called. If the subclass has stricter access to this method, when passing the subclass, it may become inaccessible

Polymorphism of java: overloading + overwriting

Question: What mechanism is used in Java Implement polymorphism?

Polymorphism is one of the object-oriented features of Java. Different manifestations of polymorphism are achieved in Java through overloading and overwriting. But there is a difference between the two.

1) Overloading

Overloading of methods in Java refers to the fact that multiple methods can be created in a class. They have the same name but different argument columns. (parameter type and number) and different definitions. The types of return values ​​can be the same or different, but overloaded functions cannot be distinguished just by different return values. When calling a method, the parameter list is used to determine which method to call. Overloading is a manifestation of polymorphism in a class.

2) Overriding

In Java, subclasses can inherit methods from parent classes by default without rewriting the same methods. However, sometimes, subclasses don’t want to Inheriting the methods from the parent class unchanged, but making certain modifications, is achieved by overriding the method (also called overwriting). Overwriting in Java refers to changing the implementation part of the same function with the same name in the parent class in the subclass, but keeping the method name, return type and parameter list of the method in the parent class consistent. That is, the subclass redefines the functions in the parent class, and the new methods in the subclass will overwrite the original methods of the parent class. Overriding is a polymorphic expression between parent class and subclass.

The above is the detailed content of Overloading and overriding of JAVA methods. 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