Back to Top

Diamond Problem in multiple inheritance

Updated 21 October 2016

In this blog I am going to explain what is diamond problem in Oops. As we know in object oriented programming inheritance is one of the most important feature and we oftenly use inheritance while working with object oriented programming. Here we are going to learn why we can not use two classes to implement this feature of object oriented programming. And why interfaces are important to implement Multiple Inheritance.

inheritance means to inherit the properties of the parent class. So that those properties could be used in the child classes.

As we know to inherit the properties of a class we need to extend that class by the child
Class ChildClass extend ParentClass {}

So if we need to inherit properties of two classes then we could extend the classes which properties we want in our child class. eg.

Class ChildClass extend ParentCLass1 extend ParentClass2 {}

Start your headless eCommerce
now.
Find out More

But there is some problem in this process and I am going to explain the biggest problem in this process. Due to which we can not extend more than one class by a class at a time. This problem is called Diamond Problem. Lets understand the process with the help of an example-

abstract Class LiveAnimals
{
   abstract public function livingPlace();

}

Class Aquatics extends LiveAnimals
{      
    public function livingPlace()
    {
       echo "Live in water";
    }
}

Class Terrestrials extends LiveAnimals
{
    public function livingPlace()
    {
       echo "Live on Land";
    }
}

Class Amphibians extends Terrestrials extends Aquatics
{
   public function live()
   {
       $this->livingPlace();
   }
}

Lets have a look at the following image which describes the process how diamond problem occurs and why it is called diamond problem because of its shape is like a diamond.

diamond problem in multiple inherince

diamond problem

Now you can see class Terrestrials and class Aquatics override a method livingPlace() and give different definitions in each class. And Class Amphibians extends class Terrestrials and classAquatics and it calls a method livingPlace() .But Class Amphibians  does not have any method names livingPlace(). So this method will be called from the parent classes of Amphibians class. But this method resides in both extended classes by Amphibians class. So in this case there is an ambiguity in calling the method livingPlace(). There is an ambiguity which extended class’s method will be called. This is called Diamond problem as problem have a structure like diamond. You can see the structure in the above diagram which looks like diamond, that’s why it is called diamond problem.

Because of this problem we can not extend two classes for implementing multiple inheritance and to resolve this problem of multiple inheritance in object oriented programming we now use interfaces for implementing the functionality of multiple inheritance. As we know we do not define a function but only declare that function in an interface. So if we use interfaces we can extend one class and one or more interfaces or we can implement more than one interfaces at a time to use the functionality of multiple inheritance and we can escape from diamond problem.

 

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • Thakur Amit Chauhan
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home