Newbie
Newbie

Reputation: 2999

Implicit inheritance working in Java

Can anyone tell me how the implicit inheritance works in java internally?
What I mean is if I create a class how exactly it extends the Object class in the JVM?

Thanks in advance.

Upvotes: 1

Views: 2132

Answers (3)

Stephen C
Stephen C

Reputation: 718826

Apart from the Object class, every class in Java must have a super-class.

There is nothing special about implicit inheritance. It is simply a syntactic shortcut that means you don't have to write extends Object. At a semantic level, implicit inheritance works exactly the same way as explicit inheritance.

In practice, this means that every class inherits certain standard methods from Object ... unless the methods are overridden. Examples include equals(Object), hashcode() and toString() which are frequently overridden, and getClass() that cannot be overridden.

Upvotes: 1

alf
alf

Reputation: 8513

For all practical reasons, you can think that class X { is a syntax sugar for class X extends Object {—that's it.

Upvotes: 1

Saša Šijak
Saša Šijak

Reputation: 9311

Java forces inheritance on every class. If you do not explicitly inherit from a class, then by default Java assumes that you are inheriting from the class called Object, which does not do much, but does have several useful methods :

  1. it implies that every class is descended from Object, since whatever class you inherit from must have inherited from something, which would be either Object or something that inherited from something, etc.
  2. the concept of polymorphism implies that you could store any type of object in a variable whose type was Object

Upvotes: 4

Related Questions