Multiple Inheritance and class Object

后端 未结 5 1297
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 00:21

I am pretty new to OOP. We all know that Java does not support multiple inheritance; however, all Java classes inherit from Object and can also inherit from ano

5条回答
  •  渐次进展
    2020-12-04 00:47

    It's not multiple inheritance it's multi level inheritance. Classes can extend one other class, which can extend one other class, ..., which ultimately extends Object:

    A --> B --> C --> Object
    

    Multiple inheritance would be

    A ----> B 
      \
       \--> C
    

    This means that when a method or a field is used inside A, it's looked up in A, then in B, then in C, then in Object.

    With multiple inheritance, it would have to be looked up in A, then in B and C, and there could be a conflict because the same method or field could exist in both superclasses.

提交回复
热议问题