Home > Java > javaTutorial > Do Java Subclasses Inherit Static Methods?

Do Java Subclasses Inherit Static Methods?

Patricia Arquette
Release: 2024-12-11 06:15:09
Original
625 people have browsed it

Do Java Subclasses Inherit Static Methods?

Static Method Inheritance in Java: Clarifying the Book's Explanation

Despite the assertion in the book that static methods are not inherited, Java allows subclasses to inherit all accessible methods, including static ones. The example provided in the question exemplifies this behavior, as the static method display() is accessible in the subclass B without the use of super.

This inheritance stems from the fundamental rule of Java inheritance, which states that a subclass inherits all public, protected, and (within the same package) package-private members of its superclass. The distinction lies in how static and non-static methods interact with newly defined methods in subclasses.

When a subclass defines a new non-static method with the same signature as an inherited one, it overrides the parent's method. However, with static methods, defining a new method in the subclass only hides the parent's method. This is because static methods are bound to the class itself, while non-static methods are associated with instances of the class.

Therefore, the book's explanation primarily pertains to non-static methods, which inherit accessibility according to their declared visibility (public, protected, or package-private). Static methods, on the other hand, adhere to the broader rule of inheritance and are available to subclasses as long as they are accessible.

The above is the detailed content of Do Java Subclasses Inherit Static Methods?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template