Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

14 Answers 14

up vote 8 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

On the top of all to keep the language design simple

And the example from the blog I follow regularly.

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.

share|improve this answer

Have a look at this StackOverflow answer: http://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.

share|improve this answer

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.

share|improve this answer

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)

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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).

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com?utm_source=stackoverflow.com&amp;utm_medium=site-ui&amp;utm_campaign=footerlink\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a>                   with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.11.25.2047\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </no"], "copyright__copyright__copyright": ["d 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.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}