2

Lets us take instances of two classes

public abstract class Shapes
{
      public abstract void draw(Graphics g);

}

public class Rectangle extends Shapes
{
     public void draw(Graphics g)
     {
          //implementation of the method 
     }
}

Here the class Rectangle has extended class Shapes and implicitly it extends class Object. I know no other extension is possible, but can't we call inherited classes Shapes and Object multiple inheritance? (Since inheriting two classes is multiple inheritance from one perspective)

darcyy
  • 5,236
  • 5
  • 28
  • 41
user1720616
  • 543
  • 1
  • 6
  • 13
  • http://stackoverflow.com/questions/4452461/inheritance-in-java-and-superclassesobject-class – Habib Dec 18 '12 at 05:00
  • 2
    Multiple inheritance is when a single class inherits directly from two or more classes in one instance (not through it's ancestor). It would be like trying to do `public class Rectangle extends Shape, Point, Dimension` - which Java obviously can't do. – MadProgrammer Dec 18 '12 at 05:01
  • s/implicitly/indirectly/ :P The `Object` inheritance is from `Shapes` extending `Object`. (Every class, except `Object`, inherits from exactly one base class. If you don't specify the base class, it defaults to `Object`.) If there's a difference between `Shapes` and `Object`, `Rectangle` will always see the `Shapes` version. – cHao Dec 18 '12 at 05:02
  • Please correct your knowledge on Multiple Inheritance. What you are referring in the question is Multi-level Inheritance its not Multiple Inheritance. – codeMan Dec 18 '12 at 05:35

5 Answers5

10

It isnt multiple inheritance. You are not inheriting from Shapes and Object, you are inheriting from Shapes which is an Object.

Multiple inheritance is only if you inherit from 2 classes at once.

public class Rectangle extends Shapes, Figures

Which isnt allowed in Java.

What you are referring to is Multilevel Inheritance. Thanks @BhavikShah

Karthik T
  • 31,456
  • 5
  • 68
  • 87
2

What you describe is the definition of single inheritance. A class inherits the attributes and methods of its superclass, and all of the superclass' superclasses. There is only one single path back to the root (Object). In multiple inheritance there would be more than one path back to the root (or even multiple roots).

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
1

This is not like that Rectangle is derived from both Shapes and Object. But Rectangle is derived from Shapes and Shapes are derived from Object, therefore Rectangle is Shape as well as Object.

 Object
   |
   V
 Shapes
   |
   V
Rectangle

Therefore there is no Multiple Inheritance in Java

Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
0

Multiple inheritance means and it is impossible in java

public class A extends B, C {

}
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
0

Multiple inheritance is not possible in java directly.

However you can achieve this (to certain extent) using interfaces.

e.g.

interface A{

 void m1();

}
interface B{

  void m2();

}

class C implements A,B{

 // it has both m1 and m2;

}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
Harit Vishwakarma
  • 2,338
  • 4
  • 21
  • 36