Multi-State--java notes of three major features of object-oriented (vii)

Source: Internet
Author: User

Polymorphic:

The same entity has multiple forms at the same time
A compile-time type has a type that is used when declaring the variable, and the run-time type has an object that is actually assigned to the variable, and a polymorphic example occurs if the compile-time type differs from the run-time type:
1    classpreson{2 }3    classTeacherextendspreson{4 }5 6  Public classDemo {7      Public Static voidMain (String args[]) {8             //This line of code will produce polymorphic9Preson p =NewTeacher ();Ten     } One}
To implement a polymorphic mechanism:
the reference variable of the parent class can point to the instance object of the subclass, and the method called by the program is dynamically bound at run time, that is, the method that refers to the real instance object that the variable points
to, that is, the method of the object that is running in memory, not the method defined in the type of the reference variable.
The role of polymorphism:
The
different sub-class objects as the parent type, you can mask the differences between different sub-class objects, write generic code, and make general programming to adapt to the changing needs
Examples of inheritance-related polymorphic generation:
1     //Birds2 classbrid{3   Public voideat () {4System.out.println ("Common birds eat insects");5  }6  7 }8 //Sparrow9 classSparrowextendsbrid{Ten   Public voideat () { OneSYSTEM.OUT.PRINTLN ("I Want to eat worms"); A  } - } - //Eagle the classEagleextendsbrid{ -   Public voideat () { -System.out.println ("I am the overlord of the air, I want to eat meat"); -  } + } - //Foraging + classforaging{ A  /*Public void Feed (Sparrow Sparrow) { at sparrow.eat (); -  } - Public void Feed (Eagle Eagle) { - eagle.eat (); -  } -  */ in  /** - * The above two methods can be improved for the following method, the use of polymorphism can achieve the same effect to   */ +  //directly pass in the parent (bird) -   Public voidFeed (Brid brid) { the brid.eat (); *  } $ }Panax Notoginseng  Public classTestdemo { -   Public Static voidMain (string[] args) { theForaging foraging =Newforaging (); +   ASparrow Sparrow =NewSparrow (); the   /** + * This line of code is a manifestation of polymorphic -    * $ * The runtime looks for the actual type of method based on the actual type $    */ - foraging.feed (sparrow); -   theEagle Eagle =NewEagle (); - Foraging.feed (eagle);Wuyi   the   /** - * Eagle Eagle = new Eagle (); Wu    * - * Dog eagle = new Eagle (); About    * $ * Sparrow Sparrow = new Sparrow (); -    * - * Dog Sparrow = new Sparrow (); -    * A    * + * can also change the declaration type, the implementation of the function will not change the    */ -   $  } the}
Classification: Multi-state at classification, method overloading run-time polymorphism, method rewriting static methods cannot be overridden, but can be inherited by a quilt class. Because the override of a method is related to an object, static-modified methods and object-independent reference type conversions: upward transformation: Small---------> Large (Subclass----------> Parent Class) Auto-Convert the object class objects to the parent class variable down transformation: Big------ ---> Small (parent-----------> Subclass) Casts convert a parent object to a subclass object before it is forced to instanceof, the judgment of the object type is generally judged: instanceof Format: Object Instanc The EOF class represents the type that determines whether an object belongs to this class. Returns the result: Boolean PS: To determine if an object has no inheritance relationship with that type, you cannot use the Instanceof polymorphic time method call: When a variable of a reference type is declared as a type of the parent class, it is actually Refers to the object of the subclass (Polymorphic case) at this time the variable cannot access the subclass of its own special fields and methods if the subclass overrides the method of the parent class, then the method that is accessed by the variable at this time is actually the method combination of the subclass: A class contains a reference to another class, which we can A relationship called an inclusive relationship or "has a"

Multi-State--java notes of three major features of object-oriented (vii)

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.