However how is it possible to inherit from Object and from any other
class at the same time? Isn't that a multiple inheritance.
No this is not what happens. Not all classes directly extend from Object
class. But only the class at the top level of inheritance hierarchy extends from Object
class(implicitly). Rest of the classes lower in the hierarchy, extends from the Object
class through the super classes. And, this is what we call multi-level inheritance.
So, consider the below hierarchy: -
class A { }
class B extends A { }
In the above case, class A
is equivalent to class A extends Object
.
Secondly, what for do we need to inherit all 11 Object methods? I
could hardly imagine why do I need it them in I/O
I suspect you meant override when you say inherit. You don't need to override any method of Object
class. It's just on your requirement, whether to override any method or not. For e.g.: - You would often want to override equals()
method, so as to write custom equality test for your instances. And in that case, you should also override the hashCode()
method, to maintain the contract of equals()
and hashCode()
.
Finally JDK 8 is going to offer us default methods realization in
interfaces and if which would probably cause multiple inheritance in
Java.
What if interface A provides method a() with default realization and
interface B provides also a() method with another default realization
and our custom class C implements both interfaces and rely on default
realization - wouldn't that be Diamond of Death ?
I can't comment on this concept, because I haven't read about this thing yet. Probably, I would update the answer sometime later.