Multiple inheritance in Java or not? [closed]

2019-04-17 09:13发布

问题:

It is said, that Java language support single inheritance only. However how is it possible to inherit from Object and from any other class at the same time? Isn't that a multiple inheritance.

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 e.g.

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 ?

回答1:

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.



回答2:

However how is it possible to inherit from Object and from any other class at the same time? Isn't that a multiple inheritance.

Unless otherwise specified, a class extends Object. That is, this:

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 e.g.

  • equals() and hashCode() are used in comparisons and hash tables.
  • notify() and wait() form the low-level basis of cross-thread communication.
  • getClass() is the starting point of all reflection code.

By putting them on Object, these can be used on every object in the JVM. You can get the hash code and the class of any object, you can check for equality between any two objects, you can monitor and notify any object.

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 ?

As explained in another question here on SO (which is literally just one search for "jdk8 default methods" away):

To solve multiple inheritance issue a class implementing two interfaces providing a default implementation for the same method name and signature must provide an implementation of the method.

Thus, the class must provide its own implementation, possibly delegating to one of the default methods.



回答3:

You are correct about "Diamond of Death" but ...

This is the situation where D implements interfaces C and B and both of those implement A. Further, there is a default method defined in two or more of those interfaces.

In Java 8 they defined a way that the two default method definitions are sorted out.

As I recall, if A and B both have a default method defined, then D uses B's version since it is lower in the class hierarchy.

If B and C both have the same default method defined then there is a conflict and D will need to implement the method itself though it can do so by calling back to the version implemented in either B or C (or it can have conditions and use both in different cases).

interface A {  } 
interface B implements A { void m() default {println("In A");} } 
interface C implements A { void m() default {println("In B");} } 
}  
class D implements B, C {  
    public void m() { println("In D"); B.super.m(); } 
} 

But you can go to Oracle's pages about new features in Java 8 and read all about it. I got there by Googling "Java 8 new features". Found what I was thinking of at http://cr.openjdk.java.net/~briangoetz/lambda/Defender%20Methods%20v4.pdf



回答4:

You inherit from a class, which inherits from object. Java does not allow you to inherit from two different class chains. However, the way around this is to use interfaces.



回答5:

However how is it possible to inherit from Object and from any other class at the same time?

You cannot do this, you heard wrong.

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 e.g.

Not sure what you're talking about here.

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 ?

Don't know anything about JDK8, but you can already implement a method with the same name/type signature in two interfaces, which is probably illogical but Java allows it.



回答6:

First- everything is an Object or a primitive in Java so you have no problems here. Object is the class on the top of the hierarchy.

At the moment you can apply multiple interfaces to Java already- then you write your realization. I would imagine that in Java 8- if you define your interface, then you have the realization. If not- then some default is used. If more than one default is defined (or the method is declared by more than one interface)- no default is used. It can be quite simple.