{"id":49195,"date":"2016-10-20T17:06:46","date_gmt":"2016-10-20T17:06:46","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=49195"},"modified":"2016-10-21T05:24:44","modified_gmt":"2016-10-21T05:24:44","slug":"diamond-problem-multiple-inheritance","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/","title":{"rendered":"Diamond Problem in multiple inheritance"},"content":{"rendered":"<p><code><\/code>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\u00a0object 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 <strong>Multiple Inheritance<\/strong>.<\/p>\n<p>inheritance means to inherit the properties of the parent class. So that those properties could be used in the child classes.<\/p>\n<p>As we know to inherit the properties of a class we need to extend that class by the child<br \/>\n<code>Class ChildClass extend ParentClass {}<\/code><\/p>\n<p>So if we need to inherit properties of two classes then we could\u00a0extend the classes which properties we want in our child class. eg.<\/p>\n<p><code>Class ChildClass extend ParentCLass1 extend ParentClass2 {}<\/code><\/p>\n<p>But there is some problem in this process and I am\u00a0going 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-<\/p>\n<pre class=\"brush:php\">abstract Class LiveAnimals\r\n{\r\n   abstract public function livingPlace();\r\n\r\n}\r\n\r\nClass Aquatics extends LiveAnimals\r\n{      \r\n    public function livingPlace()\r\n    {\r\n       echo \"Live in water\";\r\n    }\r\n}\r\n\r\nClass Terrestrials extends LiveAnimals\r\n{\r\n    public function livingPlace()\r\n    {\r\n       echo \"Live on Land\";\r\n    }\r\n}\r\n\r\nClass Amphibians extends Terrestrials extends Aquatics\r\n{\r\n   public function live()\r\n   {\r\n       $this-&gt;livingPlace();\r\n   }\r\n}<\/pre>\n<p lang=\"en-GB\">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.<\/p>\n<div id=\"attachment_62561\" style=\"width: 546px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-62561\" class=\"wp-image-62561 \" src=\"http:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png\" alt=\"diamond problem in multiple inherince\" width=\"536\" height=\"234\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png 1050w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1-250x109.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1-300x131.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1-768x336.png 768w\" sizes=\"(max-width: 536px) 100vw, 536px\" loading=\"lazy\" \/><p id=\"caption-attachment-62561\" class=\"wp-caption-text\">diamond problem<\/p><\/div>\n<p lang=\"en-GB\">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\u00a0 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&#8217;s\u00a0method 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\u2019s why it is called diamond problem.<\/p>\n<p lang=\"en-GB\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0object oriented programming. Here we are going to learn why we can not use two classes to implement this <a href=\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":83,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[3826,3827,3828,3825,3829],"class_list":["post-49195","post","type-post","status-publish","format-standard","hentry","category-php","tag-diamond-problem","tag-inheritance","tag-interface","tag-multiple-inheritance","tag-object-oriented-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>diamond problem in multiple inheritance in object oriented programming<\/title>\n<meta name=\"description\" content=\"We can not extend more than one class at a time in object oriented programming because of diamond problem. We use interfaces to escape from diamond problem.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"diamond problem in multiple inheritance in object oriented programming\" \/>\n<meta property=\"og:description\" content=\"We can not extend more than one class at a time in object oriented programming because of diamond problem. We use interfaces to escape from diamond problem.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-20T17:06:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-10-21T05:24:44+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png\" \/>\n<meta name=\"author\" content=\"Sumit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sumit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\"},\"author\":{\"name\":\"Sumit\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9\"},\"headline\":\"Diamond Problem in multiple inheritance\",\"datePublished\":\"2016-10-20T17:06:46+00:00\",\"dateModified\":\"2016-10-21T05:24:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\"},\"wordCount\":454,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png\",\"keywords\":[\"diamond problem\",\"inheritance\",\"Interface\",\"multiple inheritance\",\"object oriented programming\"],\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\",\"url\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\",\"name\":\"diamond problem in multiple inheritance in object oriented programming\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png\",\"datePublished\":\"2016-10-20T17:06:46+00:00\",\"dateModified\":\"2016-10-21T05:24:44+00:00\",\"description\":\"We can not extend more than one class at a time in object oriented programming because of diamond problem. We use interfaces to escape from diamond problem.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage\",\"url\":\"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png\",\"contentUrl\":\"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Diamond Problem in multiple inheritance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9\",\"name\":\"Sumit\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Sumit\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/sumit201\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"diamond problem in multiple inheritance in object oriented programming","description":"We can not extend more than one class at a time in object oriented programming because of diamond problem. We use interfaces to escape from diamond problem.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/","og_locale":"en_US","og_type":"article","og_title":"diamond problem in multiple inheritance in object oriented programming","og_description":"We can not extend more than one class at a time in object oriented programming because of diamond problem. We use interfaces to escape from diamond problem.","og_url":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-10-20T17:06:46+00:00","article_modified_time":"2016-10-21T05:24:44+00:00","og_image":[{"url":"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png","type":"","width":"","height":""}],"author":"Sumit","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Sumit","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/"},"author":{"name":"Sumit","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9"},"headline":"Diamond Problem in multiple inheritance","datePublished":"2016-10-20T17:06:46+00:00","dateModified":"2016-10-21T05:24:44+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/"},"wordCount":454,"commentCount":1,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage"},"thumbnailUrl":"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png","keywords":["diamond problem","inheritance","Interface","multiple inheritance","object oriented programming"],"articleSection":["php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/","url":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/","name":"diamond problem in multiple inheritance in object oriented programming","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage"},"thumbnailUrl":"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png","datePublished":"2016-10-20T17:06:46+00:00","dateModified":"2016-10-21T05:24:44+00:00","description":"We can not extend more than one class at a time in object oriented programming because of diamond problem. We use interfaces to escape from diamond problem.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#primaryimage","url":"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png","contentUrl":"http:\/\/webkul.com\/blog\/wp-content\/uploads\/2016\/10\/inheritance_diamond-1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/diamond-problem-multiple-inheritance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Diamond Problem in multiple inheritance"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3e45ec35749afa62aa598a5e1766d2b9","name":"Sumit","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0e50336dc34ad31135238f210897d19d09edbdb9be2f7974a85de3ecdef16bf6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Sumit"},"url":"https:\/\/webkul.com\/blog\/author\/sumit201\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/49195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/83"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=49195"}],"version-history":[{"count":25,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/49195\/revisions"}],"predecessor-version":[{"id":62578,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/49195\/revisions\/62578"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=49195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=49195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=49195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}