10

Actually the question was asking by one of the interviewer

Que: How can you say that java is not supporting multiple inheritance? If Object class is a parent of all classes in java.

I have no answer of that question.

That means no clear idea about java concepts :-(

Ex: if A extends B

And here A is already extending Object class. right? Now how its works?

Please share your answers..

4
  • Directly you can extend only one class - or multiply interfaces. See scala for work-around :) Aug 20, 2013 at 6:50
  • This question is getting too broad. And nobody here can teach you java, oop basics and potential anti-patterns.
    – Leri
    Aug 20, 2013 at 6:59
  • 3
    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 - when you say A a = new A(); a.someAbstractOrVirtualMethodOnD() - are you talking about the B implementation of D, or the C implementation of D? So, java bans it. Aug 20, 2013 at 7:00
  • 1
    @MerlynMorgan-Graham oh.. thanks Merlyn I got it. If you write ans for it I will accept and +1 for it :-)
    – Sachin J
    Aug 20, 2013 at 7:02

14 Answers 14

13

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.

2
  • Just theoretically (I don't think it is of any use): Why don't programming languages allow something like super<A>.someAbstractOrVirtualMethodOnD() and if the base class is not provided, throw an error ("Ambiguous method call: Method X found in both base classes A and B")?
    – Ray
    Oct 25, 2013 at 14:25
  • @PacMani: Short, short answer - it violates en.wikipedia.org/wiki/Liskov_substitution_principle - which describes the only way to use inheritance that won't bite you later. Jan 27, 2014 at 19:07
8

On the top of all to keep the language design simple

And the example from the blog I follow regularly.

enter image description here

1)We have two classes B and C inheriting from A.

2)Assume that B and C are overriding an inherited method and they provide their own implementation.

3) Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, which overridden method will be used? Will it be from B or C?

Here we have an ambiguity.

Any ways to overcome this we have interfaces and Multilevel inheritance.

Edit :

And here A is already extending Object class.

That is never called as Multiple inheritance.That is called Multi level inheritance.

In Multi level ,

Many classes are involved in inheritance, but one class extends only one. The lowest subclass can make use of all its super classes' contents.

0
7

Multiple inheritance means a single class can inherit from multiple classes. In other way, it can have multiple parent classes.

For Object class example cited by the interviewer, there are two possibilities:

  1. The interviewer himself is confused about multiple parent class(multiple inheritance) and multiple child class.

  2. Or he is trying to trick you using that question

A parent class can have many child classes and that does not relate to multiple inheritance.

1
  • Thanks for reply. Please check question I have edited it with example.
    – Sachin J
    Aug 20, 2013 at 6:57
4

Have a look at this StackOverflow answer: https://stackoverflow.com/a/9790475/2619912

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

3

This is a common misconception with Java.

The way multiple inheritance works (in C++ and Python) is something like this.

Parent1    Parent2    Parent3
   |          |          |
   _______________________
              |
              v
            Child

It means that Child will inherit the attributes and methods from all the parents.

However, in Java, inheritance works like this.

  Object
    |
    v
  Child1
    |
    v
  Grandchild

So, object is the superclass of all classes, but it is not the immediate parent of all classes. Java does, however provide a way to somewhat implement multiple inheritance by the way of Interfaces

Object
  |
  v
Child   <---  Interface
  |
  v
Grandchild

Now, Grandchild will inherit methods from Child which, in turn is obligated to implement the methods defined in the interface [Unless it is an abstract class, but that is separate discussion altogether]

So, Object is the ancestor of all classes, but it is not the parent of all classes, and Java, therefore does not support multiple inheritance.

3

Multiple inheritance is where a single class can extend from multiple classes. That is not possible in java. See here: http://en.wikipedia.org/wiki/Multiple_inheritance

When you do class A extends B in Java, then A extends B only, and not Object. B in turn extends Object (or whatever else, which will eventually extend object)

3

The only resemblence of mutiple inheritance in java is Interfaces. A class can implement multiple interfaces.

Object class is not an example of multiple inheritance. May be you misinterpreted the question.

2

The answer is Java supports multi-level inheritance but not multiple inheritance.

1

Please refer this : http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

In case of Object class to other classes its not actually multiple inheritance ... As said , its where a single class can extend from multiple classes ..

1

A java class can only be a direct child of a single parent class. It can have a grandparent but no second parent. It is like having a single biological mother. You cannot have more than one biological mothers, sure you can have a grandmother.

1

Let there be a class A and class B. Now we define a class Derived. Multiple Inheritance means: class Derived can inherit both class A and class B. But this is not possible in Java. Hence it does not support Multiple Inheritance.

1

Que: How can you say that java is not supporting multiple inheritance? If Object class is a parent of all classes in java.

Object is ancestor of all classes (father, grandfather, great grandfather, etc.) but every class has only one father (if not specified, it's the Object class).

1

Multiple inheritance should allow a class to inherit multiple parent classes. But Java doesn't allow this since it might create Diamond problem

Regarding Object class being parent, and then having many classes in inheritance hierarchy, its termed as Multi level inheritance

Sidenote:

C# allows multiple inheritance by interface by allowing child to implement multiple parent type's methods separately even though they have same signature

0

First case: Suppose you create a class A without using inheritance. By default, it is derived from Object class.

Second case: Suppose you create the class B that extends class A. Class A contains all the fields from Object class, so class B will also contain them through inheritance. It is class B extends A(which extends Object), so you could say that B is a subclass for A and also for Object.

Not the answer you're looking for? Browse other questions tagged or ask your own question.