Inheritance of Static Methods in Java
Despite the claim that static methods are not inherited in Java, code demonstrates the contrary. This contradiction raises the question: are static methods truly inherited?
To understand this apparent paradox, it's essential to revisit the concept of member inheritance in Java. According to the book, accessible members of a superclass (without using "super") are considered inherited.
However, the Sun Java Tutorials provide a broader perspective:
Based on this, static methods are inherited because they are inherently public. They are accessible by their simple names and are not bound to specific instances.
The only peculiarity with inherited static methods is their behavior when new static methods with identical signatures are introduced in subclasses. In this case, the existing static method is hidden, not overridden. This behavior ensures that the static implementation specific to the subclass remains unaffected.
Therefore, the book's explanation of member inheritance applies to both instance and static methods, with the distinction that static methods are always visible and inherited if they are accessible in the superclass. Furthermore, accessing a hidden static method depends on whether the invocation occurs from the superclass or subclass.
The above is the detailed content of Are Static Methods Truly Inherited in Java?. For more information, please follow other related articles on the PHP Chinese website!