Home > Java > javaTutorial > Are Static Methods Truly Inherited in Java?

Are Static Methods Truly Inherited in Java?

DDD
Release: 2024-12-22 06:44:11
Original
934 people have browsed it

Are Static Methods Truly Inherited in Java?

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:

  • Subclasses inherit all public and protected members of their parent class, regardless of package.
  • If the subclass is in the same package, it also inherits package-private members.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template