0

I know that Java doesn't support multiple inheritance. Every class in Java inherits from java.lang.Object class. In the absence of any other explicit super class, every class is implicitly a subclass of java.lang.Object class.

So how is it possible for a class to inherit from java.lang.Object and another explicit super class when Java doesn't support multiple inheritance?

For example

class MySuperClass {
 //this is the explicit super class
}
class MySubClass extends MySuperClass {
// this is the subclass
}

How can MySubClass inherit from java.lang.Object and MySuperClass when Java doesn't support multiple inheritance?

Saathvik
  • 267
  • 4
  • 9
  • 1
    not alloweing multiple inheritance means you cannot do `class A extends B, C`. `class A extends B` and `class B extends C` is however totally fine. Thats just hierarchical inheritance not multiple inheritance. – OH GOD SPIDERS Nov 01 '17 at 09:05
  • Think of it a bit like this: You can inherit the looks of your father - and also the looks of your grandfather (as that is via your father). But that isn't multiple inheritance. However, you *cannot* also inherit the look of the guy across the road as that **would** be multiple inheritance. – ban-geoengineering Nov 01 '17 at 09:11
  • I appreciate the quick comeback :-) – GhostCat Nov 03 '17 at 13:47

2 Answers2

2

You said it yourself, in Java all class inherit from

java.lang.Object

Therefore your superclass (MySuperClass) inherit from it and so does your subclass (MySubClass)

Object <-- MySuperClass <-- MySubClass

Not allowing multi-inheritance is another concept like having

MySubClass extends MySuperClassA, MySuperClassB

In Java this will be done through interfaces!!

Allan
  • 12,117
  • 3
  • 27
  • 51
2

A extends B.

B extends C.

So A extends C implicitly. Rename C to Object and the puzzle is solved. That is how any class is in the end derived from Object.

GhostCat
  • 137,827
  • 25
  • 176
  • 248