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)

4
  • stackoverflow.com/questions/4452461/… Commented Dec 18, 2012 at 5: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. Commented Dec 18, 2012 at 5: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. Commented Dec 18, 2012 at 5:02
  • Please correct your knowledge on Multiple Inheritance. What you are referring in the question is Multi-level Inheritance its not Multiple Inheritance. Commented Dec 18, 2012 at 5:35

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. I believe the fact that Rectacle extends Shapes and Shapes extends Object is called transitive inheritance.
@gerrytan : I believe its called multilevel inheritance
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).

Comments

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

Comments

0

Multiple inheritance means and it is impossible in java

public class A extends B, C {

}

Comments

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.