In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the difference between reloading and rewriting in java, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
1. Heavy load
Overloading means that in a class, multiple methods with the same name have different parameter lists
(different types of parameters, different number of parameters or even different order of parameters)
Overloading does not require return types, either the same or different, but you cannot return types to determine whether they are overloaded or not.
For example
Package HelloWorld;public class Zuoye {public static void main (String [] args) {Zuoye z = new Zuoye (); z.fangfa1 (); z.fangfa1 ("Zhang Xu");} public void fangfa1 () {System.out.println ("method 1") } public void fangfa1 (String name) {System.out.println ("also method 1, but with parameters" + name);}} summary:
1. Overloading is a manifestation of polymorphism in a class.
two。 Overloading is a method with different parameter lists with the same name in a class.
3. Overloading cannot distinguish overloaded functions based on the same or different return types.
two。 Rewrite
1. Rewriting means that in different classes, the methods of the parent class are rewritten in the subclass, and the subclass inherits the original method of the parent class, but sometimes it does not want to inherit the original exactly the same method of the parent class, so it is rewritten when the return type, method name and parameter list are the same. However, it is important to note that the access modifier of the subclass cannot be more stringent than that of the parent class (meaning that the access modifier of the parent class is more stringent, the permissions are less, and the parent is strict).
For example
Package HelloWorld;public class Father {public static void main (String [] args) {Son s = new Son (); s.fangfa ();} public void fangfa () {System.out.println ("I'm Dad's way") }} class Son extends Father {public void fangfa () {System.out.println ("I am the son's way");}}
The returned result is
Summary:
1. Occurs between parent and subclass relationships
two。 Their return type, method name, list parameter are the same and the method body is different.
3. The subclass cannot be stricter than the parent (the father is stricter than the son).
4 rewrite methods must not throw new check exceptions or check exceptions that are broader than those declared by the rewritten method
Private is the strictest.
Private: visible within the same category. Use objects: variables, methods. Note: classes cannot be modified (external classes)
Default (that is, by default, nothing is written, no keywords are used): visible in the same package without any modifiers. Use objects: classes, interfaces, variables, methods.
Protected: visible to classes and all subclasses in the same package. Use objects: variables, methods. Note: classes (external classes) cannot be modified.
Public: visible to all classes. Using objects: classes, interfaces, variables, methods
Summarize the difference between overloading and rewriting:
Answer: both method overloading and rewriting are ways to achieve polymorphism. The difference is that overloading implements compile-time polymorphism and overrides the implemented run-time polymorphism.
Overloading occurs in the same class, they have the same method name, but have different parameter lists, overloading has no special requirements for the return type, and cannot be distinguished by the return type.
While rewriting occurs in different classes, between the subclass and the parent class, rewriting requires that the overridden methods of the subclass and the parent class have the same return type, name, and parameter list. But their methods are different. At the same time, the access modifier of the parent class is stricter than that of the child class, that is, the access scope is smaller. It is easier to access than the overridden method of the parent class, and cannot declare more exceptions than the overridden method of the parent class (Richter substitution principle).
Thank you for reading this article carefully. I hope the article "what is the difference between reloading and rewriting in java" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.