-4

Theoretically, multiple inheritance is restricted in java but java.lang.Object class implicitly extended to every class. Need clear explanation about this?

Balasubramani
  • 645
  • 3
  • 9
  • 16

6 Answers6

2

Its multilevel inheritance not multiple inheritance.

If your class is not extended by another class then it's extends java.lang.Object class.

But If your class is extends another class then that another class is not having any super class that class get extend by java.lang.Object class not your class.

Suppose initially you have class A

class A{

}

It not having any super class so it extends java.lang.Object class.

But

class B{

}

class A extends B{

}

In this scenario your class is get extends by class B and B is not extending another class so at this point B will extend java.lang.Object class.

java.lang.Object class is super class means when your custom super class will end at that time your super class extends java.lang.Object class.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
1

Not every class directly inherits/extends Object class.

If a class don't extend any other classes, then it directly extend Object class in java.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

There simply is no multiple inheritance.

All classes inherit either directly from Object (if no base class is specified) or do so indirectly by virtue of the base class itself inheriting (directly or indirectly) from Object. All instances only contain a single copy of any fields owned by their parents.

Instead of multiple inheritance, Java relies on Interfaces to allow a class to declare that it offers a set of functionality, and each class may implement multiple interfaces.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

As you stated, in Java, a subclass cannot inherit multiple superclasses (you would use interfaces instead)

However, a superclass can be inherited by multiple subclasses (such as the Object example)

Angivare
  • 467
  • 5
  • 16
0

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class. Check : Multiple Inheritance - Wiki

Object class : Many classes inherit it; it doesnt inherit from many classes.

You were getting the definition of multiple inheritance mixed up.

phoenix
  • 717
  • 1
  • 8
  • 26
0

The answer lies in how you see it. As JB Nizet says, it is multi-level inheritance and not multiple inheritance.

Example :

If you look at FileNotFoundException, then it doesn't directly extend Object . It implicitly gets the properties of Object from its parent's parent's parent i.e, Throwable - which extends Object

So, only the first level parent classes extend Object. The Subclasses of these first level classes extend Object implitly because their parents extend Object.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104