A Java class can only inherit one direct parent class, but can indirectly inherit any number of parent classes.
How many classes can a class inherit at most in Java:
A class can only inherit one Direct parent class, but this parent class can indirectly inherit multiple parent classes. Therefore, a class can inherit from any number of indirect parent classes.
Direct inheritance:
extends
keyword. Indirect inheritance:
Example:
<code class="java">class Animal { // 动物的属性和方法 } class Dog extends Animal { // 狗的属性和方法 } class GoldenRetriever extends Dog { // 金毛猎犬的属性和方法 }</code>
GoldenRetriever
Directly inherits Dog
, indirectly inherits Animal
. Dog
directly inherits Animal
. Therefore, GoldenRetriever
has access to all properties and methods defined in Animal
and Dog
.
The above is the detailed content of How many classes can a class inherit in Java?. For more information, please follow other related articles on the PHP Chinese website!