2

All classes in Java are inherited from Object class by default.

Then how the class inherited can inherit other classes?

Java do not support Multiple inheritance, right?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
AdiCoder
  • 29
  • 4
  • 6
    Because when you inherit from another class, you don't inherit directly from `Object` anymore. You'll inherit from `Object` **through** your custom class. The class still has only one parent. – Arthur Attout Dec 23 '19 at 12:36

4 Answers4

4

Classes inherit from at least and at most one class,

Either from Object implicitly (without writing extends), or from other class explicitly (as extends YourParentClass)

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

Because each class inherit from Object or from class inherit from Object, each class still inherit from Object, for example toString() method

If your custom class inherit from different class, still at the end of the hierarchy, the parent class will be Object, notice that classes hierarchy isn't multiple inheritance:

The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes

At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

As you already correctly stated, all classes in Java are inherited from Object class by default. That means that any class that does NOT have an extends clause defined, implicitly has written extends Object. Example

class Animal // implicitly written extends Object
{}

On the other hand, if a class has an extends clause, then it does not directly inherit from Object. But its parent class inherits from object. As inheritance is transitive, therefore the class also inherits the functions of Object. Example

class Dog extends Animal {}

class Animal // implicitly written extends Object
{}

This means Dog is an Animal and Animal is an Object. Therefore Dog is an Object. You could write Object o = (Object) new Dog(); and that would totally be fine.

This can go arbitrarily deep. For example you could now have a class Poodle which inherits Dog which inherits Animal which inherits Object. Therefore a Poodle is an Object.

findusl
  • 2,454
  • 8
  • 32
  • 51
0

By using inheritance , a child class will inherit object class through parent class.

Sarjit
  • 799
  • 7
  • 9
0

What you are talking about is known as - Multilevel inheritance.

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.

i.e. - A is Object class, B is the base class that you want to inherit and C is your child class.

enter image description here

btw Java 8 has introduced default method to handle basic multiple inheritance problem ( though I don't agree that it solves the actual problem) - check default methods in Java 8

freesoul
  • 236
  • 1
  • 13