Polymorphism member characteristics

Source: Internet
Author: User
Features of non-static member functions in polymorphism: ① during compilation: see whether calling methods exist in the class to which referenced variables belong. If yes, the compilation is successful; otherwise, the compilation fails. ② during runtime: see whether methods are called in the class to which the object belongs. Features of the non-static member functions in polymorphism:
① During compilation: see whether calling methods exist in the class to which the referenced variable belongs. If yes, the compilation is successful; otherwise, the compilation fails;
② Runtime: see whether methods are called in the class to which the object belongs.
--------------------------------------------------
// In short: when a member function is called, compile the function to the left and run the function to the right. //
--------------------------------------------------
2. features of member variables with the same name in polymorphism: (ps: not verified by different names)
For compilation and running, refer to the class to which the referenced variable belongs)
3. characteristics of static member functions in polymorphism:
For compilation and running, refer to the class to which the referenced variable belongs)
Example:

Class Fu {int num = 1; public void method1 () {System. out. println ("fu_1");} public void method3 () {System. out. println ("fu_3");} // The static method (including variables) does not belong to the object. it is bound to the class to which it belongs, public static void method4 () {System. out. println ("fu_4") ;}} Class Zi extends Fu {int num = 2; public void method1 () {System. out. println ("zi_1");} public void method2 () {System. out. println ("zi_2");} public static void method4 () {System. out. println ("zi_4") ;}} Class Duotaitest {public static void main (String [] args) {Fu f = new Zi (); f. method1 (); // f. method2 (); if this line of code exists, compilation fails. f is not used in the Fu class. method3 (); f. method4 (); System. out. println (f. num); Zi z = new Zi (); System. out. println (z. num);}/* running result: zi_1fu_3fu_4 // The static method cannot be overwritten by the quilt class. if you want to call the static method in the subclass (this is generally not used, but will be used for the interview) zi. method4 (class name. method name) 12 */}

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.