Static methods of the parent class in Java cannot be overridden

Source: Internet
Author: User

The static methods of the parent class in Java do not really have to be rewritten, but some people may do experiments to find that it is not a problem to rewrite the parent class static method in a subclass. Here, let me explain the details.

public class Parent {

public static void Add () {
System.out.println ("=========parent");
}
}

public class children extends Parent {

public static void Add () {
System.out.println ("=========children");
}

}

The above two classes did not find an error in the system and passed the test

public static void Main (string[] args) {
Children ch = new Children ();

Parent ch = new Children ();
Ch.add ();
}

The above test can be carried out, but attention is paid to the red callout part, the problem is here, the two quoted examples are new children (), if the static method can be rewritten, the results of the two sentences should be "=========children", But that is not the case, and both of them call their respective methods. So static methods are not rewritten, and their methods are independent.

Static methods of the parent class in Java cannot be overridden

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.