Home> Java> javaTutorial> body text

What is the difference between rewriting and overloading in java

青灯夜游
Release: 2020-10-19 11:07:13
Original
17151 people have browsed it

Difference: Rewriting is when a subclass rewrites the implementation process of a method that allows access to the parent class. The method name and parameter list are the same, and the return value and formal parameters cannot be changed. Overloading is when multiple functions of the same name with different number or types of parameters exist in the same class at the same time, with the same method name and different parameter lists.

What is the difference between rewriting and overloading in java

Related recommendations: "Java Video Tutorial"

##Q: What are Java overloading and rewriting? What's the difference?

Answer:


Overload(Overload) is a means for a class to process different types of data in a unified way. Functions with the same namewith different number or types of parameters (the return value type can be arbitrary, and the return type cannot be used as the criterion for distinguishing overloaded functions) exist in the same class at the same time, which is polymorphism in a class A manifestation of polymorphism (when calling a method,determines which method to use by passing different number of parameters and parameter types).

What is the difference between rewriting and overloading in java

Figure 1. An overloading example in an Android class
What is the difference between rewriting and overloading in java
Override

(Override) is the parent class and The essence of polymorphism between subclasses is to redefine the functions of the parent class. If a method defined in a subclass has the same name and parameters as its parent class, the method will be rewritten, but the access of the subclass function The modification permission cannot be less than that of the parent class; 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 will overwrite the original method. If the original method in the parent class is required The method can use the super keyword.

What is the difference between rewriting and overloading in java

Figure 2. A common overriding example in Android classes
What is the difference between rewriting and overloading in java----------

** Overloading rules: ** must have different parameter lists; can have different return types; can have different access modifiers; can throw different exceptions.

Rewriting rules:

The parameter list must be exactly the same as the overridden method, otherwise it cannot be called an override; the return type must always be the same as the overridden method , otherwise it cannot be called an override;The access modifier limit must be greater than or equal to the access modifier of the overridden method; the overridden method must not throw a new checked exception or be broader than the overridden method declaration Checked exceptions, for example, the parent class method declares a checked exception IOException. When overriding this method, you cannot throw Exception. You can only throw subclass exceptions of IOException, and you can throw unchecked exceptions.

Overloading and rewriting are different manifestations ofJava polymorphism.
Rewriting is a manifestation of polymorphism between the parent class and the subclass, which works at runtime (dynamic polymorphism, such as dynamic binding)
Overloading is a manifestation of polymorphism in a class, which takes effect at compile time (static polymorphism, such as static binding).


Q: Can Java constructors be overridden and overloaded?Answer:

Overriding is a method in which a subclass method overrides a parent class method. The overridden method name remains unchanged, and the class constructor method name must be consistent with the class name. Assuming that if the construction method of the parent class can be overridden by the subclass, the class name of the subclass must be consistent with the class name of the parent class, so

Java's construction method
cannot be overridden. The overloading is for the same one, so the construction methodcan be overloaded.

Q: What is the result of the following program and why?

public class Demo { public boolean equals( Demo other) { System.out.println("use Demo equals." ); return true; } public static void main(String[] args) { Object o1 =new Demo (); Object o2 =new Demo (); Demo o3 =new Demo (); Demo o4 =new Demo (); if (o1.equals(o2)) { System.out.println("o1 is equal with o2."); } if(o3.equals(o4)) { System.out.println("o3 is equal with o4."); } }}
Copy after login
Answer: The results of the above program are as follows.

use Demo equals. o3 is equal with o4.
Copy after login
Because the public boolean equals(Demo other) method in the Demo class does not override the public boolean equals(Object obj) method in the Object class, the reason is that it violates the parameter rules, one of which is the Demo type The other one is of type Object, so these two methods are in an overloading relationship (occurring at compile time) rather than an overriding relationship;

So when o1.equals(o2) is called, o2 is an Object type parameter, in fact The public boolean equals(Object obj) method

in the Object class is called, because at compile time o1 and o2 are both Object types, and the

equals methodof the Object class returns false by comparing the memory address. ;When o3.equals(o4) is called, the equals(Demo other) method in the Demo class is actually called. Because o3 and o4 are both of Demo type at compile time, the above printing is there.

The above is the detailed content of What is the difference between rewriting and overloading 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!