1

If every class in Java implicitly extends Object class and multiple inheritance is not possible in Java then how do we even extend any class?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bhushan
  • 354
  • 9
  • 25
  • Because single inheritancy is chained from one class to the next. Multiple inheritancy deals with extending from two or more objects within a single generation – MadProgrammer Apr 11 '13 at 06:01
  • 1
    Read about difference between multiple and multi level inheritance. – prashant Apr 11 '13 at 06:03
  • http://stackoverflow.com/questions/13926555/does-java-not-support-multiple-inheritance and http://stackoverflow.com/questions/7553201/java-doubt-on-multiple-inheritance – Habib Apr 11 '13 at 06:12
  • @Habib I don't have any confusion regarding Multiple or Multi-level inheritance. Anyways got my doubt cleared.Thanx – Bhushan Apr 11 '13 at 06:24

5 Answers5

3

If you extend a class A that class in turn extends Object, so B extends A implicitly also extends Object.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
3

"Java doesn't have multiple inheritance" means that you can't have two different parents, not that your parent can't have a parent.

C++ is an example of a language that lets you do multiple inheritance: http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/

Multiple inheritance looks like this:

class Teacher: public Person, public Employee

which means 'Teacher extends Person and Employee, inheriting its fields and methods'.

Instead of multiple inheritance, you're expected to create and implement interfaces to represent all the behaviours (or contracts if you prefer) your object supports. Java uses this for interfaces like Closeable and Serializable.

Patashu
  • 21,443
  • 3
  • 45
  • 53
1

Every Class that dosn't extends any other class it extends Object class. if you extends anther class example extends Vector class look to the hierarchy of class Vector you will end with a simple class that dosn't extends any anther class which explicitly extends Object. and Any class extends anther class it explicitly extends all class that the parent class extends.

aymankoo
  • 653
  • 6
  • 12
1

Multiple Inheritance is the concept of a single class inheriting from two or more super classes. If a class inherits from a super class and if that super class inherits from another super class it does NOT qualify as Multiple Inheritance. It is still Single Inheritance.

once u create object of sub class the object hierachy would be create on order of

objectclaass–>superclass—>subclass;

It is true that every class in Java inherits from the Object class – either indirectly or directly.

so in this case the sub class indirectly inherits object class.

Vijendra Singh
  • 628
  • 3
  • 13
0

There is no multiple inheritance in Java, but there is class hierarchy. Inheritance in Java is transitive: if class A extends Object and class B extends A, than by transitivity A extends Object.

Amir Kost
  • 2,148
  • 1
  • 16
  • 30