JAVA_SE basics-49. Application of Polymorphism

Source: Internet
Author: User

JAVA_SE basics-49. Application of Polymorphism

Because of the importance of polymorphism for future development, I am dedicated to opening a multi-state application here to further explain it. I hope that students who want to understand polymorphism can read it patiently.

After learning about object polymorphism, what is the usefulness of this polymorphism? The following describes how to design A method that can accept any subclass object of Class A and call the method. If object polymorphism is not used, the code will certainly be similar to code1.

 

Code1 (Object polymorphism is not used for implementation)

 

Class A {// defines the class Apublic void Run1 () {// defines the Run1 () method System. out. println (Class A ---- Run1 method);} public void Run2 () {Run1 (); // call Run1 () of the parent class () method} class B extends A {public void Run1 () {// override the Run1 Method System in the parent class. out. println (class B ---- Run1 method);} public void Run3 () {// method unique to Class B System. out. println (class B ---- Run3 method);} class C extends A {public void Run1 () {// override the Run1 Method System in the parent class. out. println (C Class ---- Run1 method);} public void Run5 () {// the unique method of class C System. out. println (C class ---- Run5 method);} public class Demo6 {public static void main (String asrgs []) {Run (new B ()); // transfer B class instance B = new B (); Run (new C (); // transfer C class instance C c = new C ();} public static void Run (B B) {B. run1 (); // call the override Run1 () method in the parent class} public static void Run (C c) {c. run1 (); // call the override Run1 () method in the parent class }}
Running result:

 


 

 

Although the above code1 code implements the basic requirements, you should find that if you complete the program in code1 mode, this is equivalent to running () when A subclass of Class A is generated () the method needs to be reloaded once, and the class itself needs to be modified for every extension subclass. This is certainly not good. What if the object polymorphism is used to complete it? The specific code is code2.

 

Code2 (implemented using object polymorphism)

 

Class A {// defines the class Apublic void Run1 () {// defines the Run1 () method System. out. println (Class A ---- Run1 method);} public void Run2 () {Run1 (); // call Run1 () of the parent class () method} class B extends A {public void Run1 () {// override the Run1 Method System in the parent class. out. println (class B ---- Run1 method);} public void Run3 () {// method unique to Class B System. out. println (class B ---- Run3 method);} class C extends A {public void Run1 () {// override the Run1 Method System in the parent class. out. println (C Class ---- Run1 method);} public void Run5 () {// the unique method of class C System. out. println (C class ---- Run5 method);} public class Demo6 {public static void main (String asrgs []) {Run (new B ()); // transfer class B instances to generate an instance that transfers Class C up (new C (); // transfer class C instances, generate up transfer} // receive the parent class Object public static void Run (A a) {// equivalent to A a = new B () & A a = new C (). run1 (); // call the override Run1 () method in the parent class }}
Running result:

 


 

In the code above, code2 uses the object polymorphism in the Run () method, so it can receive objects of any subclass, so no matter how the subclass is increased, the Run () method does not need to be changed, after an object is transformed upwards, the called method must be a method that has been overwritten by the quilt class.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.