Inheriting an Object Class in Java

While I was reading a java book, I came across " Every class extends the class Object " ... but if you want class B to extend class A ..... but class B will now have multiple inheritance, one from class Object and one of class A. When the conflict is resolved. Can someone explain?

+4
source share
5 answers

First of all, the Object class is the super / base / parent class for each class, including custom classes.

Thus, even if we do not specify it explicitly, user classes extend the Object class by default.

Morevoer, the Object class, implements a set of methods and variables that are common to all objects created in the application. This is the main reason we have the Object class as the base class for all other classes.

For instance,

hashCode() - This method creates a unique identifier for each object created in the JVM.

+1
source

Multilevel inheritance, not several:

class A extends Object

class B extends A

+5
source

no conflict .. look at this structure

  • animal
    • bird
      • sparrow
      • parrot
    • dog
      • poodle
    • cat

the parrot class receives all the attributes / methods of its superclass bird and from its superclass animal. this is called multiple inheritance.

Do you get traits from your parents? You also get traits from your parents.

+1
source

ClassB is distributed from ClassA, which is also distributed from Object. Therefore, ClassB extends Object indirectly through classA

"Each class extends the Object class" means that if you do not specify a parent class, it accepts the object as a parent class

+1
source

The book tried to explain that each class is either a direct or indirect subclass of Object . Among other things, this means that each class inherits the public methods of Object : toString() , hashcode() , wait() , etc. It also means that any variable of class a is always there, you can always assign a to a variable of class Object .

There is no such thing as multiple inheritance in Java. The next Java comes to the fact that these are interfaces, which are the subject in itself.

+1
source

Source: https://habr.com/ru/post/1385836/


All Articles