0

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 ?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
gabriel angelos
  • 349
  • 1
  • 9
  • 23
  • 8
    Please don't take any offense in this, but what you're implying is that your grandparents also your parents. – Benjamin Gruenbaum Feb 04 '13 at 17:57
  • If "A" is an interface it can not provide a default implementation of method a(). – km1 Feb 04 '13 at 17:59
  • 2
    "Multiple inheritance" refers to a model where a class can have multiple *parents*, not multiple *ancestors*. It has nothing to do with the number of methods that a class inherits from any ancestor classes. – chepner Feb 04 '13 at 18:22

6 Answers6

8

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    Every class extends Object, per the Java spec. – hd1 Feb 04 '13 at 17:57
  • Only answers the first question. – Paul Bellora Feb 04 '13 at 17:59
  • @hd1. Ah! Actually, missed the critical part. Updated now. – Rohit Jain Feb 04 '13 at 18:00
  • @PaulBellora.. Takes some time to update answer right? – Rohit Jain Feb 04 '13 at 18:02
  • Neither override nor inherit. But I don't need neither of those 11 methods at all for writing or reading bytes into / from a stream. In this case what for do I need Object on the top of my class hierarchy ? – gabriel angelos Feb 04 '13 at 18:02
  • To write generic code for example (Lists etc). – bjedrzejewski Feb 04 '13 at 18:04
  • 3
    @user2033357: It's not as if having those 11 methods has any cost. It doesn't take up any extra memory or anything. – Louis Wasserman Feb 04 '13 at 18:05
  • @user2033357 Java relies on MANY of those "not needed" methods under the hood. Removing them would make a Class stop being an Object meaning that either you should specify how should it be handled on certain situations OR Java should be a millenary sage that knows how do you expect the class to behave... for example when representing it as a String (`toString` method won't be there for you). – Fritz Feb 04 '13 at 18:07
  • 1
    @user2033357. It's not that you need some method for a particular task or not. Behind the scenes, those methods are used. Simplest example is `hashcode` method. Whenever you create an instance of a class, the `default implementation` of `hashcode` is used to generate the `hash codes` for the instance, if you don't override one. So, hashcode will anyhow be needed. Similarly, other methods are used under the hood, that you don't get to know early on. – Rohit Jain Feb 04 '13 at 18:10
  • @Gamb Or whatever those objects support wouldn't apply to your class, which *can* make sense (e.g. all those `wait` and `notify` fluff, `finalize`, and `clone` don't seem to make sense for many kinds of objects). `getClass` is special anyway and could be exposed differently (e.g. a static method of `Class`). One could also argue that `hashCode`, `equals`, and `toString` are not *universally* necessary, though they are more widely useful. –  Feb 04 '13 at 18:11
3

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.

Community
  • 1
  • 1
Mattias Buelens
  • 19,609
  • 4
  • 45
  • 51
1

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.

enter image description here

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

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
0

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.

hd1
  • 33,938
  • 5
  • 80
  • 91
0

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.

Harold R. Eason
  • 563
  • 2
  • 9
0

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.

bjedrzejewski
  • 2,378
  • 2
  • 25
  • 46