Inheritance of static variables and static methods in Java

Source: Internet
Author: User

Class A {    static int a = 1;    static int b = 2;    public static void PrintA () {        System.out.println (a);    }    public static void Printb () {        System.out.println (b);    }} Class B extends a {    static int a = 3;    static int b = 4;    public static void Printb () {        //if B is not redefined in B, it will be called a.b, after redefinition, is called b.b        System.out.println (b);    }} public class Test {public static void main (string[] args) {///b inherits static methods from a, note that although the value of a is also defined in B (B.A), the Printa called is a method in a,// He prints the value of a static variable A (A.A) in A, B.printa (); [1]//if PRINTB () is not defined in B, it will be called PRINTB () in a, but because B is also defined in B's printb (), it is called//b's own method, so the value of the static variable B (b.b) in B is printed as well B.PRINTB (); [2]//B can also call a directly, if there is no redefinition in B, call the A variable inherited from a, redefine it, call its own aSystem.out.println (B.A);}}


Printing results:

1

4

3

Summarize:

1. Static variables and static methods say inheritance is not accurate, static methods and variables are the methods and variables belonging to the class. Subclasses also belong to the superclass, such as manage extends employee, then manage is also an employee, so subclasses can call static variables and methods that belong to the superclass. Notice that the subclass calls the static methods and variables of the superclass rather than the static methods and variables that inherit from the Super class. But if there are static methods and variables with the same name in the subclass, then the subclass itself is called, because the static and static methods of the subclass hide the static methods and variables of the parent class.

2. If a variable and method with the same name is not defined in the subclass, then calling the "subclass name. static method/variable" calls the methods and variables of the parent class.

3,. If the subclass has only a static variable with the same name defined, and no static method with the same name as the parent class is called, the "subclass name. Static method" is called when the static method of the parent class is invoked, and the static variable in the static method is also the parent class (such as note [1] in the program).

4. If the subclass defines a static variable with the same name as the parent class, and a static method with the same name as the parent class, then calling the "subclass name. Static method" is completely unrelated to the parent class, and the static variable is also a subclass (such as note [2] in the program).


Inheritance of static variables and static methods in Java

Related Article

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.