Understanding of the "multi-state" static method

Source: Internet
Author: User

It should be emphasized that polymorphism occurs only in methods, but not in domains. For methods, only non-static and non-final methods have polymorphism.

The static method does not have polymorphism. it is worth noting that the so-called static method cannot be polymorphism: whether the override method itself is static, not whether the method that calls the override method is static!
For example, the following program:

 

Class father ...{
Public void act ()...{
System. Out. println ("father. Act ()");
}
}

Class son extends father ...{
Public void act ()...{
System. Out. println ("son. Act ()");
}
}
Public class test ...{
Public static void test (father I )...{
I. Act ();
}
Public static void main (string [] )...{
Son son = new son ();
Test (son );
}
}

The output result is son. Act (). If static cannot be polymorphism, shouldn't it be father. Act?

In fact, as mentioned above, we should consider whether the Act () method is static, whether there is polymorphism, rather than whether the called method test () is static.
This is irrelevant to whether the test method is static. If both act methods are modified to static, the output result is Father. Act (), because the static method cannot be polymorphism.

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.