Java:If a Extends B and B Extends Object, Is That Multiple Inheritance

Java : If A extends B and B extends Object, is that multiple inheritance

My answer is correct?

Yes, mostly, and certainly in the context you describe. This is not multiple inheritance:

Object ^- ClassA ^- ClassB

It's what you said it is, single inheritance with multiple levels.

This is multiple inheritance: Inheriting from two or more bases that don't have any "is a" relationship with each other; that would be inheriting from unrelated lines, or from lines that had previously diverged (in Java, since Object is always a base, it would be the latter):

Object ^- ClassA, Object ^- ClassB, ClassA ^- ClassC, ClassB ^- ClassC

(Image credits: http://yuml.me in "scruffy" mode)

Internally What happens actually?

Just what you said: There are multiple levels. When the compiler is resolving a member on an instance:

obj.member

...it looks to see if the type of obj (which in this case is a class, say ClassB) has member, either because it provides it directly or it has it through inheritance. At runtime, the JVM uses the member the object actually has.


The reason I said "mostly" above is that Java has interfaces, and as of Java 8 it has "default methods" on interfaces. This complicates things a bit, but your answer about levels is correct in the context of what you described the interviewer saying about Object, ClassA, and ClassB.

Interfaces have always made it possible, in Java, for something to have an "is a" relationship with two different types: A class type it inherits from, and any of several interface types it implements. Interfaces without default methods aren't multiple inheritance in a practical way (the class has to provide the implementation), but they did make it possible for a class to have multiple "is a" relationships from unrelated type trees. (I'm not an academic, it's possible an academic would argue that they provide multiple inheritance in an academic way.)

With Java 8, interfaces can provide default implementations of the methods they define, which really blurs the lines even at the practical level. Let's look at that a bit more deeply:

Say we have ClassA:

class ClassA {
void doSomething() {
// Code here
}
}

and Interface1:

interface Interface1 {
default void doSomethingElse() { // Requires Java 8
// Code here
}
}

and finally ClassB:

class ClassB extends ClassA implements Interface1 {
}

ClassB inherits the implementation of doSomething from ClassA. But it also gets the "default" version of doSomethingElse from Interface1. We didn't implement it in ClassB, but ClassB isn't abstract: It really has doSomethingElse. It gets it from the interface. I used the word "gets" rather than "inherits" there, but this looks a lot like inheriting the default method.

This is basically multiple-inheritance "light" (as in "light beer"). It does an end-run around the thornier problems with true multiple inheritance, like:

  • What should the type of super be? (Java 8's answer: ClassA)
  • What order do you run constructors in? (Java 8's answer: Single-lineage constructor chaining, interfaces don't have constructors.)
  • Do you run constructors that you inherit more than once, more than once? (Java 8's answer: You can't inherit constructors more than once, interfaces don't have them.)
  • What happens if you inherit multiple methods with the same signature? (Java 8's answer: If one of them is from the base class, that's the one that's used; a base class's implementation can override the default method of multiple interfaces. If you have multiple default methods with the same signature from different interfaces at compile-time, it's a compile-time error. If an interface has been changed without the class being recompiled and the situation arises at runtime, it's a runtime IncompatibleClassChangeError exception listing the conflicting default methods.)

Multiple Inheritance in Java since All classes extend from Object class?

All classes extend form Object, either implicitly or explicitly, directly or indirectly, the whole class hierarchy in Java ends up pointing at Object, which is at the root. For instance, when you write this:

public class MyClass extends Object {

Is exactly the same as this:

public class MyClass {

And if we have this:

public class MySubClass extends MyClass {

Then MySubClass extends from MyClass which extends from Object. It's a transitive inheritance relationship, and it occurs in only one direction: at no point in the hierarchy it will be possible that a single class extends from more than one class - that's why we say that in Java we have single-inheritance (as opposed to: multiple-inheritance.)

Java doesn't support multiple inheritance but implicitly every class in java extends Object and allows one more

Your class that extends that other class, but it extends Object, too, so you're still in one line of inheritance, not two.

Is that Considered Multiple Inheritance

Answer NO.

In java only exist simple-inheritance.

public class A {  // Extends from `Object` class
}

class B extends A { // Extends from `A`
}

class C extends B { // Extends from `B`
}

What you have here is

C is a B , B is a A , A is an Object

(C is A by transitivity)

Multiple inheritance would be

C is a B and also is a D, D is an object, B is a A , A is an Object

In java is not allowed multiple inheritance (having more than one parent).

What you can do is implements multiple interfaces and there you can have a kind of multiple inheritance.

How to explain multiple inheritance in Java

Multiple inheritance is about multiple-direct-inheritance.

A single class class can't have two immediate parent classes. It can have a grandparent class, though.

A extends B and B extends C, is not the same as A extends both B and C.

The reason this is disallowed is for simplicity when you have a case like:

A extends both B and C

B extends D

C extends D

If you had such a case, and then you had this code:

A a = new A();
a.someAbstractOrVirtualMethodOnD();

... are you talking about the B implementation of someAbstractOrVirtualMethodOnD(), or the C implementation of that same method? Which should get called? (Hint: there isn't a great answer)

So, Java bans it.

Note, you can get something much like multiple inheritance if you implement multiple interfaces. But since there is only one concrete implementation, there is no confusion as to what gets called.

Multiple Inheritence Related Confusion in Java

But, every class of Java inherits Object class by default, and if we extend a class then there are two inherited classes first is Object and second is our inherited class

That's partially correct.

If you extend a class explicitely, then it won't extend Object class.

if you have a class

class A
{

}

then, compiler will change it into

class A extends Object
{

}

But, if you extends a class explicitely, as following,

class A extends AnotherClass
{

}

compiler won't add anything now, hence No multiple inheritance.



Related Topics



Leave a reply



Submit