Javase-java polymorphism

Source: Internet
Author: User

/** Object-oriented features: Polymorphism * 1. Polymorphism can be understood as multiple manifestations of a thing * 1) method overloading and rewriting * 2) Subclass Object Polymorphism (Main) * 2. Prerequisites for the polymorphism of the subclass object: 1) Occurrence of class inheritance 2) to have a subclass override of the parent class Method 3) The parent class reference points to the subclass object * 3. The program is divided into compilation state and running state * 1) for polymorphism, compile, "look to the left", this reference variable is understood as the type of the parent class, so the subclass unique methods and properties can not call * 2) Runtime, "Look right", Focus on the real entity of the object: the object of the subclass, then the method of execution is the subclass rewrite of * 4.instanceof* 1) Format: Object A instanceof Class A: Determine whether object A is an instance of Class A, if yes, return true; otherwise return false* 2) If a is an instance of Class A, then a must also be an instance of the parent class of Class A: Object A instanceof Class A's parent class, the result is true* 3) if a class is inherited by more than one class, it is important to note that the sub-class cannot convert between 5. Polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can take many different behaviors depending on the sending object (sending the message is the function call).
Polymorphism refers to the specific type of reference variable defined in the program and the method calls made through the reference variable are not deterministic during programming, but are determined during the program's run.
That is, an instance object that refers to the class to which a reference variable is inverted, and the method call that the reference variable emits is the method that is implemented in the class, and must be determined by the time the program is run. * 6. The technique of implementing polymorphism is called dynamic binding, which is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type. * 7. Polymorphic effect: eliminates coupling between types. * 8. When invoking a method using polymorphic mode, first check whether the method is in the parent class, if not, compile the error, and if so, then call the subclass of the same name method. * */public class Parent {public static void main (String args[]) {//person p=new person (),//P.eat ();//P.walk ();////man M =new man ();//M.eat ();//M.Walk ();//Sub-class object polymorphism: The reference of the parent class to the subclass object (the subclass object is assigned to the parent class reference) person P1=new man ();//upward transformation// Virtual method invocation: Refers to the object entity of the subclass through the reference of the parent class, when calling a method with the same name as the child class and the parent class, the method P1.eat ();p 1.walk () of the subclass overriding the parent class is actually executed,//p1.entertainment (); No method defined for the type person Entertainment (), the program thinks P1 is person, no entertainment () Method Man m1= (man) p1;//down Transformation m1.entertainment ();//Woman w1= (Woman) p1;/ /compile without errors, run with error java.lang.ClassCastException, men become women//w1.shopping ();//Woman w1= (Woman) New Man ();//error occurred at compile time// Avoid the above error if (P1 instanceof Woman) {Woman w1= (Woman) p1;w1.shopping ();} if (P1 instanceof man) {mans m2= (man) m1;m1.entertainment ();}} If there is no polymorphism public void Show (person p) {}public void Show (man p) {}public void Show (Woman p) {}//polymorphism requires only one Show method Public void Show (Person p) {}} public class Person {private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public person () {super ();} Public person (String Name,int age) {super (); this.name=name;this.age=age;} public void Walk () {System.out.println ("man walks!") ");} public void Eat () {System.out.println ("people eat!") ");}} public class Mans extends Person{private Boolean smoking;public man () {super ();} Public Man (Boolean smoking) {super (); this.smoking=smoking;} public Boolean issmoking () {return smoking;} public void Setsmoking (Boolean smoking) {this.smoking = smoking;} public void Walk () {System.out.println ("The man walks in a straight walk!") ");} public void Eat () {System.out.println ("A man eats a big mouth!") ");} public void Entertainment () {System.out.println ("The Man Loves the Treat!") ");}} public class Woman extends Person{private boolean isbeauty;public Woman () {super ();} Public Woman (Boolean isbeauty) {super (); this.isbeauty=isbeAuty;} public Boolean isbeauty () {return isbeauty;} public void Setbeauty (Boolean isbeauty) {this.isbeauty = isbeauty;} public void Walk () {System.out.println ("The woman walks with a slim fit!") ");} public void Eat () {System.out.println ("A Woman eats shyly!") ");} public void Shopping () {System.out.println ("Woman loves shopping!") ");}}

  

Javase-java polymorphism

Related Article

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.