Reputation: 2999
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
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
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
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 :
Upvotes: 4