-1

I know that every class in java extends Object class by default. Which means that whenever a class extends a class it actually extends two classes? Or whenever a class extends another class it doesnot extends object since the class which it extends already extends object ?

Smrita
  • 1,259
  • 3
  • 18
  • 38
  • 1
    You can say it extends both classes if you wish to think of it that way, but so long as the two classes have a parent-child relationship then it doesn't really matter. "Multiple inheritance" (which C++ supports) is where a class inherits from two (or more) classes which are NOT in a parent-child relationship. It produces some mind-boggling situations (and is a bear to implement efficiently as well). Java uses interfaces to achieve some of the features of multiple inheritance without actually having it. – Hot Licks Sep 10 '14 at 01:53
  • The key to this is *'by default'.* If you extend another class, the default doesn't apply, right? – user207421 Sep 10 '14 at 02:10
  • You have a Doctor class which extend Person. Person extends Object and Doctor extend Person. if a Doctor is a person, and a Person is a object then, a Doctor is also a Object. – silfrede Sep 10 '14 at 02:12

1 Answers1

2

No, if it extends another class, it extends a class which extends Object (or a different class if the hierarchy is deeper). Each class has a single direct super-class.

Similarly, if class A extends B, and B extends C, A only has one direct super-class - B - even though C is an in-direct super-class of A.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Okay got it. I mean I just wanted to make sure:) – Smrita Sep 10 '14 at 01:48
  • You have a Doctor class which extend Person. Person extends Object and Doctor extend Person. if a Doctor is a person, and a Person is a object then, a Doctor is also a Object. – silfrede Sep 10 '14 at 02:08
  • @silfrede I'm assuming you are referring to the class that extends another class other than Object, and not `the class that you extend` (which already extends `Object` directly). In that case, yes, the sub-class also extends `Object`, but indirectly. – Eran Sep 10 '14 at 02:12
  • You have a Doctor class which extend Person. Person extends Object and Doctor extend Person. if a Doctor is a person, and a Person is a object then, a Doctor is also a Object. – silfrede Sep 10 '14 at 02:12