Java static methods cannot be overwritten

Source: Internet
Author: User

// static methods cannot be overwritten

?

/*class super{

???? Static String name () {

???????? return "Mother";

????}

}

?

Class test02{

???? public static void Main (string[] args) {

???????? Super S3 = new super ();

???????? System.out.println (S3.name ());

????}

}

*/

?

/*

* Result : Mother

*/

?

/*

Class super{

???? Static String name () {

???????? return "Mother";

????}

}

?

Class Sub extends super{

???? String name () {

???????? return "Baby";

????}

}

?

Class test02{

???? public static void Main (string[] args) {

???????? Super S1 = new Sub ();

???????? System.out.println (S1.name ());????????

????}

}

*/

?

Result:mother

?

/*

Class super{

???? String name () {

???????? return "Mother";

????}

}

?

Class Sub extends super{

???? Static String name () {

???????? return "Baby";

????}

}

?

Class test02{

???? public static void Main (string[] args) {

???????? Super S1 = new Sub ();

???????? System.out.println (S1.name ());????????

????}

}

*/

?

Result:mother

?

class super{

???? static String name () {

???????? return"mother";

????}

}

?

class Sub extends super{

???? static String name () {

???????? return"Baby";

????}

}

?

class test02{

???? Public staticvoid main (string[] args) {

???????? Super S = new super ();

???????? System. out. println (S.name());????????

???????? Super S1 = new Sub ();

???????? /* The run-time is a pointer to The class of Sub,s1.name () should output "Baby" . But the output mother * *

???????? System. out. println (s1. Name());

???????? /*

???????? * The result is: Mother

???????? * " A static method is determined at compile time based on the class to which the method is called and the classes that the object belongs to

???????? * The instance method is determined at run time based on the class to which the object belongs " How do you understand that?

???????? */

????}

}

?

Java static methods cannot be overwritten

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.