Java Foundation 12-polymorphism is a feature of members

Source: Internet
Author: User

Java Foundation 12-polymorphism is a feature of a member

1, member variable.

Both compilation and run refer to the left side of the equals sign.

Overrides occur only on functions, and variables do not matter.

Fu f = new Zi ();
System.out.println (f.num);//is the parent class, the answer is 3

2, member function (non-static).

Compile to see left, run to see right.

Because the member function has an override attribute.

Fu f = new Zi ();//
F.show ();
The output is the show method inside the subclass

3, static function.

Compile and run all looking to the left.

Static functions are not polymorphic, polymorphism is the polymorphism of objects, and then static functions do not involve objects.

Fu f = new Zi ();//
F.show ();
The last output here is the contents of the parent class show.

Zi z = new Zi ();//
Z.show ();

The output is the show in the sub-class

Second, examples
1 /*2 Polymorphic ,3 features of the members:4 1, member variable. 5 compile time: Refer to whether the referenced variable belongs to a class that has a calling member variable, have, compile through, no, compile failed. 6 Runtime: Refer to whether the referenced variable belongs to a class that has a calling member variable, and run the member variable in the owning class. 7 Simply put: both compile and run refer to the left side of the equals sign. Oh, yes. 8 as an understanding. 9 overrides occur only on functions, and variables do not matter. Ten Fu f = new Zi (); One System.out.println (f.num);//is the parent class, the answer is 3 A not according to the value of f (the address of the subclass object) to find, but according to the type of f to find.  - It is not possible to develop such a situation, my parent class has, my sub-class is directly used, and the use of the time has generally been downward transformation.  -  the  -  -  - 2, member function (non-static).  + compile-time: refers to whether there are functions called in the class to which the referenced variable belongs. There, compile through, no, compile failed.  - Runtime: Refers to whether there are functions called in the class to which the object belongs.  + Simply put: Compile to look to the left and run to see the right.  A  at because the member function has an override attribute.  - Fu f = new Zi ();// - f.show (); - The output is the show method inside the subclass - depends on the object, has the object has the member function, must dynamically bind to the specified object, therefore runs the time to look at the subclass, but compiles the time to check the language -  in error, so when compiling, check the parent class, so look at the parent class.  - compilation checks for syntax errors, which run at the address that the reference points to.  to  +  -  the  * 3, static function.  $ compile-time: refer to whether there are static methods called in the class to which the referenced variable belongs. Panax Notoginseng Runtime: Refers to whether there are static methods called in the class to which the referenced variable belongs.  - simply put, compile and run all look to the left.  the  + in fact, for static methods, the object is not required. It can be called directly with the class name.  A Fu f = new Zi ();// the f.show (); + The last output here is the contents of the parent's show, because the static member does not need the object, is directly referred to by the class name, and points to the method area where the static method is stored . -  $ and that is the father's show.  $ Zi z = new Zi ();// - z.show (); - The Zi here is inherited from Fu, the Show method is static the The output is the show in the sub-class - It can be understood that static functions are not polymorphic, polymorphism is the polymorphism of objects, and then static functions do not involve objectsWuyi Parent Class object reference, which is a static function that points to the parent class the the Subclass object reference, which is the object function that points to the child class -  Wu  -          About  $ */ -  - classFu - { A //int num = 3; +     voidShow () the     { -System.out.println ("Fu Show"); $     } the  the     Static voidmethod () the     { theSystem.out.println ("Fu static Method"); -     } in } the  the classZiextendsFu About { the //int num = 4; the     voidShow () the     { +System.out.println ("Zi Show"); -     } the Bayi     Static voidmethod () the     { theSystem.out.println ("Zi static Method"); -     } - } the  the  the  the classDuoTaiDemo3 - { the      Public Static voidMain (string[] args) the     { the Fu.method ();94 Zi.method (); the //The essence of this is that the parent object points to a subclass reference, which is a bit like a pointer, and the value of F is the address of the subclass object.  theFu f =NewZi ();// the //F.method ();//the output is the static of the parent class98 //f.show ();//Compile the time to check the parent class, run when the subclass of the main, show is overwritten, run the sub-class show About         //The output is a subclass of show, - //System.out.println (f.num);//is the parent class, the answer is 3.101 102 103 //Zi z = new Zi ();104 //System.out.println (z.num);//is a subclass, the answer is 4. the     }106}

Three, memory storage analysis

Fu f = new Zi ();

Fu F defines a reference, which is a pointer, in the stack.

New Zi () defines an object in the heap, except that the object has a part member of the parent class.

1. If a subclass reference is used to point to this object, all access is subclass.

2, if the parent class reference to this object, all access to the object inside the parent class, but the parent function is overwritten, so that the member is the parent class, the function when the subclass.

It must be based on the pointer type to access what you want to access. The cat must go to eat cat food, the dog eats the dog food.

Java Foundation 12-polymorphism is a feature of members

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.