In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain "Java rewriting, reloading, how to define the use of polymorphism", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java rewriting, reloading, how to define the use of polymorphism"!
1. Rewrite: a. Definition:
Rewriting is that the subclass rewrites the implementation of the method that the parent class is allowed to access, and the return value and formal parameters cannot be changed. That is, the shell remains unchanged and the core is rewritten!
The advantage of rewriting is that subclasses can define their own specific behavior as needed. In other words, the subclass can implement the methods of the parent class as needed.
An overridden method cannot throw a new check exception or an exception that is broader than the overridden method declaration. Example:
Class Animal {
Public void move () {
System.out.println ("Animals can move")
}}
Class Dog extends Animal {
Public void move () {
System.out.println ("dogs can run and walk")
}}
Then we quote:
Animal a = new Animal (); / / Animal object
Animal b = new Dog (); / / Dog object
A.move (); / / execute the method of the Animal class
B.move (); / / execute the method of the Dog class
The output is:
Animals can move. Dogs can run and walk.
As you can see in the above example, although b is of type Animal, it runs the move method of the Dog class (the method we overridden)
This is because during the compilation phase, you only check the reference type of the parameter.
At run time, however, the Java virtual machine (JVM) specifies the type of object and runs the method of the object.
So in the above example, the compilation is successful because there are move methods in the Animal class, but at run time, you are running object-specific methods.
b. Rules:
1. The parameter list must be exactly the same as that of the overridden method.
two。 The return type can be different from the return type of the overridden method, but it must be a derivative of the return value of the parent class.
3. Access cannot be lower than that of a method overridden in the parent class.
4. The member method of a parent class can only be overridden by its subclasses. If subclasses in the same package can override all parent methods, except those declared as private and final.
5. Methods declared as final cannot be overridden, while methods of static cannot be overridden, but can be declared again.
6. If the subclass and the parent class are not in the same package, the subclass can only override the non-final methods declared as public and protected of the parent class.
7. An overridden method can throw any unforced exception, regardless of whether the overridden method throws an exception or not. However, an overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than that declared by the rewritten method, or vice versa.
two。 Overload:
It's about the same as C++.
Overloading (overloading) is in a class with the same method name but different parameters. The return type can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
The most common place is the overloading of constructors.
What is different from rewriting is that overloading focuses on different parameters.
3. Polymorphism: a. Definition:
Polymorphism is the ability of the same behavior to have many different forms or forms of expression. Polymorphism is the same interface that uses different instances to perform different operations.
It is important to learn the concept of polymorphism to apply interfaces well.
Three necessary conditions for the existence of polymorphism:
1. Inherit
two。 Rewrite
3. The parent class reference points to the subclass object
For example, this example points a reference to a parent class to an object of a subclass:
Parent p = new Child ()
Note: when calling a method in a polymorphic manner, first check whether the method exists in the parent class, if not, compile the error; if so, call the method of the child class with the same name.
b. Virtual function:
Virtual functions are one of the bases of polymorphism. Of course, all functions in JAVA are virtual functions in the sense of C++ by default, so don't pay special attention to them, because dynamic binding is the default behavior of Java.
If you don't want a function to be virtual in Java, you can add the final keyword to become a non-virtual function.
At this point, I believe that you have a deeper understanding of "Java rewriting, reloading, how to define the use of polymorphism," might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.