Home  >  Article  >  Java  >  Can static properties and static methods in Java be inherited?

Can static properties and static methods in Java be inherited?

王林
王林Original
2020-01-16 09:52:343560browse

Can static properties and static methods in Java be inherited?

Conclusion:

Static properties and static methods in Java can be inherited, but they are not overwritten, but hidden.

Cause analysis:

1. Static methods and attributes belong to classes. When calling, they can be completed directly through class name.method name, no need The inheritance mechanism can then be invoked.

If static methods and attributes are defined in the subclass, then the static methods or attributes of the parent class are called "hidden" at this time. If you want to call the static methods and properties of the parent class, you can directly use parent class name.method or variable name. As for whether to inherit, subclasses can inherit static methods. and properties, but they are not the same as instance methods and properties. There is such a situation as "hidden".

(Online learning video tutorial sharing: java video tutorial)

2. The reason why polymorphism can be realized depends on inheritance, interface and rewriting and overloading (inheritance and rewriting are most critical). With inheritance and overriding, references from the parent class can point to objects of different subclasses.

The function of rewriting is: after "rewriting", the priority of the subclass is higher than the priority of the parent class, but "hiding" does not have this priority.

3. Static properties, static methods and non-static properties can be inherited and hidden but cannot be overridden. Therefore, polymorphism cannot be achieved, and references from parent classes cannot point to objects of different subclasses. Non-static methods can be inherited and overridden, thus enabling polymorphism.

Recommended related articles and tutorials: java introductory tutorial

The above is the detailed content of Can static properties and static methods in Java be inherited?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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