Multiple Inheritance and class Object

后端 未结 5 1294
佛祖请我去吃肉
佛祖请我去吃肉 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.

    0 讨论(0)
  • 2020-12-04 00:48

    what you explain is multilevel inheritance that allowed in java.

    enter image description here

    but multiple inheritance not allowed in java.

    enter image description here

    0 讨论(0)
  • 2020-12-04 00:53

    Your class that extends that other class, but it extends Object, too, so you're still in one line of inheritance, not multiple inheritance.

    It calls as a multi-level inheritance. not multiple inheritance.

    0 讨论(0)
  • 2020-12-04 00:56

    Its still multi level inheritance. If u would use multiple inheritance in java - use intefrace for these purposes.

    Because:

    Java support only multiple interface inheritance, and java does not support multiple inheritance
    
    0 讨论(0)
  • 2020-12-04 00:58

    That is not multiple inheritance ....That is multi level inheritance in java

    Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

    enter image description here

    0 讨论(0)
提交回复
热议问题