Polymorphism (iii)

Source: Internet
Author: User

classfu{intnum = 5; voidmethod1 () {System.out.println ("Fu Method_1"); }    voidmethod2 () {System.out.println ("Fu Method_2"); }    Static voidmethod4 () {System.out.println ("Fu Method_4"); }}classZiextendsfu{intnum = 8; voidmethod1 () {System.out.println ("Zi Method_1"); }    voidmethod3 () {System.out.println ("Zi Method_3"); }    Static voidmethod4 () {System.out.println ("Zi Method_4"); }}

Fu f = new Zi ();
System.out.println (f.num);//Print 5
F.METHOD4 ();//Print Zi Method_4

Summarize:

1. Characteristics of member functions in polymorphism:
At compile time: see if there is a method called in the class to which the referenced variable belongs. If there is, compile through, if no compilation fails
At run time: see if there is a method called in the class to which the object belongs
The simple summary is: member function in polymorphic call, compile look to the left, run to see the right

2. In polymorphism, the characteristics of member variables: both compile and run, refer to the left (the class to which the reference variable belongs)

3. In polymorphic, the characteristics of static member functions: both compile and run, refer to the left

Polymorphism (iii)

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.