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)06/02 Report--
This article introduces the relevant knowledge of "what is the use of Java polymorphism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A little bit of eye contact
Upward transition: or transition to a parent class. The object of the parent class is instantiated by the subclass object, which is actually the upward transformation of the object. Upward transformation does not require forced type casting, but upward transformation loses precision.
Transition down: or transition to a subclass. That is, a parent class object can be converted to a subclass object, but a cast must be performed at this point.
During the upward transition, the methods that can be seen by the parent object are still the methods defined in this class (that is, methods overridden by subclasses). If the subclass extends some new methods, the parent class is invisible.
The extension method of the second parent class object can not find the subclass.
1 code
Class baseClass {public void print () {System.out.println ("*-parent baseClass:public void print () {}");} class subClass extends baseClass {public void print () / / method overrides {System.out.println ("#-subclass subClass:public void print () {}") } public void getB () / / this method extends the subclass function {System.out.println ("#-subclass subClass:public void getB () {}, class B extension method.") ;} public class NewMethodTest {public static void main (String args []) {baseClass baseObj = new subClass (); / / instantiate the subclass object baseObj.print (); baseObj.getB (); / / this method parent class cannot be found}}
2 run
Compilation failed
3 description
Although the instantiation of the parent object baseObj depends on the subclass, what it can see is the name of the method defined by its own class. if the method is overridden by the subclass, the called method body is also the method overridden by the subclass.
Three downward transformation
1 code
Class baseClass {public void print () {System.out.println ("*-parent baseClass:public void print () {}");} class subClass extends baseClass {public void print () / / method overrides {System.out.println ("#-subclass subClass:print ()") } public void getB () / / this method extends the function of the subclass {System.out.println ("#-subclass subClass:getB (), subclass extension method.") ;} public class DownCastTest {public static void main (String args []) {baseClass baseObj = new subClass (); / / instantiate the subclass object baseObj.print (); / / call the print () subClass subObj = (subClass) baseObj of the subclass subClass; / / transform down to force the completion of subObj.getB (); / / this method parent class cannot be found, but the subclass object can be found}}
2 run
#-subclass subClass:print () #-subclass subClass:getB (), subclass extension method.
3 description
Formally, the object defined by the class can only see the members of the class to which it belongs, although the subclass object can assign values to the parent class object through upward type conversion. however, the parent class object can only see the members that are overridden in the subclass (these methods are also defined in the parent class), and the parent class object cannot see the new extension method of the subclass.
Four hidden
1 finishing touch
In some scenarios, we do not want the method of the parent class to be overridden by the method of the subclass, that is, after the subclass is instantiated, it will call the method of the parent class instead of the method of the subclass. Static methods modified by the keyword static cannot be overridden, and Java uses this feature to achieve a hidden effect.
2 code
Class Father {public static void overWritting () {System.out.println ("#-Father method");}} class Son extends Father {public static void overWritting () {System.out.println ("*-- Son method");}} public class HideSubClass {public static void main (String args []) {Father dad = new Son (); dad.overWritting (); Father.overWritting (); Son.overWritting ();}}
3 run
#-Father method#--Father method*--Son method
This is the end of the content of "what is the use of Java Polymorphism". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.