Conclusion: static attributes and static methods in java can be inherited, but not overwritten. cause: 1 ). static methods and attributes belong to the class and are called directly by the class name. the method name is correct and does not require an inheritance mechanism or can be called. If static methods and attributes are defined in the subclass, the static methods or attributes of the parent class are called "hidden ". If you want to call the static methods and attributes of the parent class, you can directly use the parent class name. the method or variable name is complete. As to whether to inherit, subclass inherits static methods and attributes, but it is not the same as instance methods and attributes. This situation is "hidden. 2). polymorphism depends on inheritance, interfaces, rewriting, and overloading (Inheritance and rewriting are the most critical ). With inheritance and rewriting, the parent class can be referenced to objects of different subclasses. The rewrite function is: after "Rewriting", the priority of the subclass is higher than that of the parent class, but "hidden" does not have this priority. 3 ). static attributes, static methods, and non-static attributes can be inherited and hidden, but cannot be overwritten. Therefore, they cannot realize polymorphism and reference of parent classes can direct to objects of different subclass. Non-static methods can be inherited and overwritten, so polymorphism can be implemented.
Example:
Package com. study. test; public class A {// parent class public static String staticStr = "A static attribute"; public String nonStaticStr = "A non-static attribute"; public static void staticMethod () {System. out. println ("A static method");} public void nonStaticMethod () {System. out. println ("A non-static method ");}}
Package com. study. test; public class B extends A {// subclass Bpublic static String staticStr = "B static attributes after rewriting"; public String nonStaticStr = "B non-static attributes after rewriting "; public static void staticMethod () {System. out. println ("B static method after rewriting ");}}
Package com. study. test; public class C extends A {// subclass C inherits all attributes and methods in}
Package com. study. test; public class StaticExtendsTest {public static void main (String [] args) {C c = new C (); System. out. println (c. nonStaticStr); System. out. println (c. staticStr); c. staticMethod (); // The output results are non-static attributes, static attributes, and static methods in the parent class. Static attributes and static methods can be inherited from System. out. println ("-------------------------------"); A c1 = new C (); System. out. println (c1.nonStaticStr); System. out. println (c1.staticStr); c1.staticMethod (); // The result is the same as the preceding one. The output results are non-static attributes, static attributes, and static methods in the parent class, static attributes and static methods can be inherited from System. out. println ("-------------------------------"); B B B = new B (); System. out. println (B. nonStaticStr); System. out. println (B. staticStr); B. staticMethod (); System. out. println ("-------------------------------"); A b1 = new B (); System. out. println (b1.nonStaticStr); System. out. println (b1.staticStr); b1.staticMethod (); // The result is a static method of the parent class, indicating that the static method cannot be overwritten and cannot implement polymorphism }}