-1

The Oracle docs states very clearly that Java Language has no Multiple Inheritance feature i.e. Any class declared in Java cannot inherit features from two separate classes. But there lies a statement that Class Object is the super class of every other class created by user. Consider a sample Class as follows:

Class Base {
.....
}

Class Derived extends Base {
.....
}

Now if we look onto Class Derived then it is clearly visible that it inherits from Class Base and also from Class Object, then that proves that Java does has Multiple Inheritance.

Solution as provided from one of the pages of Oracle Sites

Excepting Object, which has no superclass, 
every class has one and only one direct superclass (single inheritance). 
In the absence of any other explicit superclass, 
every class is implicitly a subclass of Object.

This means that any class which doesn't have a single-inheritance available will only have an implicit single-inheritance with Class Object.

Refer http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Nandhan
  • 195
  • 1
  • 1
  • 8
  • 2
    It is Multilevel inheritance not Multiple Inheritance. – Ankur Lathi Jan 08 '14 at 06:59
  • 1
    No my friend... Class Derived doesnt directly extend Object... The Highest Level parent of class Derived extends Object. i.e, here Base extends object. – TheLostMind Jan 08 '14 at 06:59
  • I can't see any thing like multiple inheritance here. We normally call multiple inheritance if one class has more than one parent class. `Java` never ever had that. – Ruchira Gayan Ranaweera Jan 08 '14 at 06:59
  • `Derived -> Base -> Object` – Narendra Pathai Jan 08 '14 at 07:00
  • Java does not have multiple inheritance. `Base` inherits from `Object`, if it isn't one of the primitive types (or an enum) then it is an Object in Java. – Elliott Frisch Jan 08 '14 at 07:00
  • possible duplicate of [Multiple Inheritance](http://stackoverflow.com/questions/3466783/multiple-inheritance) – Brian Roach Jan 08 '14 at 07:03
  • @gowtham - Please remember that interfaces were not introduced to support multiple inheritance in Java. – TheLostMind Jan 08 '14 at 07:06
  • ya Agreed and corrected myself – gowtham Jan 08 '14 at 07:07
  • My question is mistaken. Before I get further down votes, I would like to share a material read from Oracle Sites http://www.oracle.com/technetwork/java/object-142075.html#6681 "Ultimately, all classes eventually inherit from the Object class, forming a tree structure with Object as its root." Oracle sites clearly state that every class created by user has class Object as its superclass. – Nandhan Jan 08 '14 at 07:25
  • Please find the solution in the question section – Nandhan Jan 08 '14 at 07:48

5 Answers5

2

One could modify your code to

class Base extends Object{
.....
}

class Derived extends Base {
.....
}

So since only Base (and not Derived) extends Object, the class Derived inherits Object through Base, no multiple inheritance exists that way.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
claj
  • 5,172
  • 2
  • 27
  • 30
0

If one class has(extends ) more than one parent class we called it as multiple inheritance. In Java never ever had that, In this case just a multi level inheritance not multiple inheritance.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

This is a case of Multi-Level Inheritance not Multiple Inheritance.

Multiple Inheritance : we can inherit more than one class in the same class.

enter image description here

Multi-Level Inheritance: where one class can inherit only one base class and the derived class can become base class of some other class

enter image description here

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

In Java, you can't extend from 2 separate class hierarchies. However, you can have multiple classes within a hierarchy. One class definition can have only one "extends" in it.

For example, Mammal extends Animal and Dog extends Mammal is acceptable. Dog will get the behaviors and states of Mammal and Animal (through Mammal).

What is not allowed is one class extending multiple classes.

So in your example, if you have 2 base classes BaseA and BaseB, class Derived cannot extend both BaseA and BaseB at the same time.

user3170817
  • 131
  • 3
0

I think you're misunderstanding inheritance.

Multiple inheritance allows a class to inherit the properties of two distinct classes. From Wikipedia, here is an illustration of one such hierarchy (known as the "diamond problem"):

enter image description here

Take note: D is inheriting from both B and C.

To bring it to a better illustration, multiple inheritance would allow you to have a class structure like this:

public class Bike {
    // properties of a bike
}

public class Car {
    // properties of a car, namely that it has a motor
}

public class Moped extends Bike, Car {
    // properties inherited combine that it is both a Bike and a Car
}

The current way that Java classes are wired now is that their inheritance is hierarchal; that is, one class may only ever have one parent class.

A good way to think of inheritance in this manner is the "is-a" relationship test.

For your example:

  • A Derived is a Base.
  • A Base is an Object.
  • A Derived is an Object.
Makoto
  • 104,088
  • 27
  • 192
  • 230