In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "what is the concept of Java polymorphism?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the concept of Java polymorphism" article.
1. The concept of polymorphism
To put it simply, different objects complete the same behavior, but produce different states.
A very simple example: the students of a certain class in the school take the same exam, and after the exam, different students have different test scores, some fail, some walk up the passing line, and some have high marks, so what level are you at ~ (▽) ~
The realization of polymorphism requires conditions, which are as follows:
1. Must be under the inheritance system, and the upward transformation has taken place.
two。 The subclass needs to override the methods of the parent class
3. Call the override method through a reference to the parent class
Some of the new concepts involved above are described in more detail below
two。 Upward Transformation 2.1 concept
Let's make some changes to the class we wrote earlier:
Class Plant {String name; String source; String genu; public void func () {System.out.println ("parent class method");}} class Tree extends Plant {public void trait () {System.out.println ("subclass method");}}
The so-called upward transformation is to create a subclass object and use it as a parent class.
The specific operations are as follows:
Plant tree=new Tree ()
Because it is used as a parent class, tree cannot call the methods of the subclass at this time
There is a downward transition in Java, but it is skipped because it is not safe.
2.2 Common forms of upward transformation
In the above example is the first: direct assignment
Besides, there are two forms.
As a parameter of the method
As the return value of the method
3. Rewrite
That is, the subclass rewrites the method of the parent class. This method cannot be a constructor, nor can it be modified by private, final and static. What we rewrite is the content of the method, and the method name, return value and parameters of the method cannot be modified.
Still make changes to the class:
Class Plant {String name; String source; String genu; public void trait () {System.out.println ("parent method");} class Tree extends Plant {@ Override / / comment, which can be used to verify the validity of public void trait () {System.out.println ("subclass method");}}
Let's take a look at the result of calling trait after the upward transformation:
Methods of subclasses are called, which is also a feature of overrides
In fact, when compiling, the method of the parent class is indeed called, but the rewritten method determines which method to call at run time (that is, dynamic binding), which is also the essential difference between overriding and overloading.
3.1 considerations for rewriting
1.static, final, private modification methods and construction methods cannot be overridden
two。 The access permission of the subclass method is greater than or equal to the parent class.
3. After JDK7, the overridden method can return a different value type, but it must have a parent-child relationship, which is a covariant type
4. Do not call overridden methods in constructors
The last point is explained separately: suppose the constructor of the parent class calls the overridden method, because the constructor of the subclass will first call the constructor of the parent class. therefore, the rewritten method is dynamically bound and called before the subclass object is fully constructed, in which case the probability of program problems will be greatly increased.
After understanding upward transformation and rewriting, the industry basically knows how to achieve polymorphism in Java: the combination of upward transformation and rewriting to achieve polymorphism under the inheritance system.
The above is the content of this article on "what is the concept of Java polymorphism". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.