Inheritance in Java language

2020-03-31 04:37发布

We say that java follows a single inheritance model i.e. a Java class can extend only one class at max. and then say that every java class is inherited from Object class.

Suppose there are two classes A and B. Both A and B extend from Object. Now suppose A extends B. Doesn't it imply that A has multiple inheritence (A is inheriting from both B and Class Object)?

9条回答
▲ chillily
2楼-- · 2020-03-31 05:11

A Java class can only directly inherit from one class. In this case, A doesn't directly inherit from Object, only from A.

Are you solely asking from the perspective of terminology, or is there some behaviour you're interested in?

查看更多
Emotional °昔
3楼-- · 2020-03-31 05:11

In Java, each class can directly extend only from one parent class. So, either B extends from Object and A extends from B, xor both A and B extend from Object.

You are supposing that both is true at the same time - this is not possible in Java.

If B extends from Object, and A extends from B, then yes, A does inherit from Object, but there is still a single parent to each and every class:

class Object
 |
 +-- class B
      |
      +-- class A

In other words, Java doesn't support multiple inheritance.

查看更多
做个烂人
4楼-- · 2020-03-31 05:14

You can have an inheritance chain, this is not multiple inheritance. You cannot have a class that inherits from more than one class at a time.

Forbidden by the language:

class A extends B, C
{}
查看更多
登录 后发表回答