{"id":1771,"date":"2019-12-26T06:10:43","date_gmt":"2019-12-26T06:10:43","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=1771"},"modified":"2019-12-26T11:27:46","modified_gmt":"2019-12-26T11:27:46","slug":"inheritance-and-access-modifiers","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/","title":{"rendered":"Inheritance and Access Modifiers"},"content":{"rendered":"<h2>Introduction to Inheritance:<\/h2>\n<p>A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones. Similarly, inheritance is a feature of OOP, which allows making use of an existing class without making changes to it. This can be achieved by deriving a new class (Derived class) from the existing class (Base class). More properties can be added to the derived class if needed. Therefore, the complexity of derived class may grow as the level of inheritance grows.<br \/>\n<strong>\u201cThe mechanism of driving a new class from the older one is called Inheritance. The old class is called the Base class or Super Class or Parent Class and the new one is called the Subclass or Derived Class or Child Class.\u201d<\/strong><\/p>\n<p>For example, the Programmer is a class in itself. It is also a member of an Employee class as shown below:<br \/>\n<img decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/12\/example-01.png\" alt=\"example-01\"\/><\/p>\n<h3>Benefits:<\/h3>\n<p>OOP provides several benefits to the programmer. The principal advantages are: <\/p>\n<ul>\n<li>We can eliminate redundant code.<\/li>\n<li>We can extend the use of existing classes.<\/li>\n<li>It is easy to partition the work in the form of objects. <\/li>\n<li>Complexity can be easily managed.<\/li>\n<li>We can build programs using standard working modules that communicate with one another.  This leads to the saving of our development time and higher productivity.<\/li>\n<li>It is possible to map multiple objects in the program.<\/li>\n<li>The principle of data hiding helps the programmer to build a secure program that can\u2019t be invaded by code in other parts of the program.<\/li>\n<li>Message passing techniques are used for passing the data between two objects.<\/li>\n<\/ul>\n<h3><strong>Access Control:<\/strong><\/h3>\n<p>Access Control provides a restriction for accessing the members of a class. It determines how a member can be accessed. The following access specifiers provided by PHP are:<\/p>\n<ul>\n<li>Public Access<\/li>\n<li>Private Access<\/li>\n<li>Protected Access<\/li>\n<\/ul>\n<p><strong>Public Access: <\/strong><br \/>\nA variable or method declared as the public has the widest possible visibility and accessible everywhere. The Public member of a class is visible or accessible to its own class, its derived classes and outside the class.<\/p>\n<p><strong>Private Access:<\/strong><br \/>\nThe Private members of the class can be accessed within their own class. They cannot be inherited by subclasses and are therefore inaccessible in subclasses.<\/p>\n<p><strong>Protected Access:<\/strong><br \/>\nClass members are declared as protected when they can be accessed only within the class itself and by inheriting classes. <\/p>\n<h3><strong>Types of Inheritance:<\/strong><\/h3>\n<ul>\n<li>Single <\/li>\n<li>Multiple <strong>(Not Supported in PHP)<\/strong><\/li>\n<li>Hierarchical <\/li>\n<li>Multi-level <\/li>\n<li>Hybrid <strong>(Not Supported in PHP)<\/strong><\/li>\n<\/ul>\n<h3>Single Inheritance:<\/h3>\n<p>When one class extends the properties of Another class then we call it a single inheritance. In the below example, A is a <strong>parent class<\/strong> of B and B would be a <strong>child class<\/strong> of A.<\/p>\n<p><img decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/12\/example-02.png\" alt=\"single inheritance\"\/><\/p>\n<h3><strong>Multiple Inheritance:<\/strong><\/h3>\n<p>In this , a class can inherit the properties of more than one parent class. For example;  <strong>A, B<\/strong> are <strong>parent classes<\/strong> of C and C would be a <strong>child class<\/strong> of A and B.<\/p>\n<p><strong>PHP doesn\u2019t support multiple inheritances but using Traits in PHP instead of classes, we can implement it.<\/strong><br \/>\n<img decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/12\/example-03.png\" alt=\"Multiple Inheritance\"\/><\/p>\n<h3><strong>Hierarchical Inheritance:<\/strong><\/h3>\n<p>When multiple classes inherit the properties of the same class then it is called hierarchical inheritance. In this kind of inheritance one class is inherited by multiple subclasses. In below example class B, C and D inherit the same class A. A is parent class (or base class) of B, C &#038; D.<br \/>\n<img decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/12\/example-04.png\" alt=\"Hierarchical Inheritance\"\/><\/p>\n<h3><strong>Multilevel Inheritance:<\/strong><\/h3>\n<p>In this inheritance, We derived new classes at different levels. When a class is derived from a class which is also derived from another one, i.e. a class having more than one parent class, such <strong>inheritance <\/strong>is called <strong>Multilevel Inheritance<\/strong>. As shown in the flow diagram C is a subclass or child class of B and B is a child class of A.<br \/>\n<img decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/12\/example-05.png\" alt=\"Multilevel_Inherit\"\/><\/p>\n<h3><strong>Hybrid Inheritance:<\/strong><\/h3>\n<p>Hybrid inheritance is a combination of multilevel and multiple inheritance. This can be achieved by combining more than one type of inheritance. A hybrid inheritance can be achieved in <a href=\"https:\/\/studysection.zohosites.in\/\">PHP<\/a> in the same way as multiple inheritances. Using Traits one can have multiple as well as a hybrid <a href=\"https:\/\/studysection.com\/blog\/php-best-practices\/\">inheritance in PHP<\/a>.<br \/>\n<strong>PHP doesn\u2019t support Hybrid type but using Traits in PHP instead of classes, we can implement it.<\/strong><br \/>\n<img decoding=\"async\" src=\"..\/wp-content\/uploads\/2019\/12\/example-06.png\" alt=\"Hybrid_inherit\" \/><br \/>\n<small><em>PHP programming is a valuable skill that a programmer can have. Let StudySection help you out with proving your programming skills through its <a href=\"https:\/\/studysection.com\/php-web-development-foundation\">PHP certification<\/a>. StudySection provides beginner as well as expert-level certifications in PHP programming to prove your level of skills in PHP programming.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Inheritance: A child inherits the property of his parent. He can Acquire new properties or modify the inherited<\/p>\n","protected":false},"author":1,"featured_media":1792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[316,313,314,315],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Inheritance and Access Modifiers<\/title>\n<meta name=\"description\" content=\"A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones.Similarly, inheritance is a feature of OOP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Inheritance and Access Modifiers\" \/>\n<meta property=\"og:description\" content=\"A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones.Similarly, inheritance is a feature of OOP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-26T06:10:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-26T11:27:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/12\/modifiers.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Inheritance and Access Modifiers\",\"datePublished\":\"2019-12-26T06:10:43+00:00\",\"dateModified\":\"2019-12-26T11:27:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\"},\"wordCount\":733,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Access\",\"Inheritance\",\"Modifiers\",\"OOP\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\",\"url\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\",\"name\":\"StudySection Blog - Inheritance and Access Modifiers\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2019-12-26T06:10:43+00:00\",\"dateModified\":\"2019-12-26T11:27:46+00:00\",\"description\":\"A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones.Similarly, inheritance is a feature of OOP.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inheritance and Access Modifiers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Inheritance and Access Modifiers","description":"A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones.Similarly, inheritance is a feature of OOP.","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:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Inheritance and Access Modifiers","og_description":"A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones.Similarly, inheritance is a feature of OOP.","og_url":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2019-12-26T06:10:43+00:00","article_modified_time":"2019-12-26T11:27:46+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/12\/modifiers.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Inheritance and Access Modifiers","datePublished":"2019-12-26T06:10:43+00:00","dateModified":"2019-12-26T11:27:46+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/"},"wordCount":733,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Access","Inheritance","Modifiers","OOP"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/","url":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/","name":"StudySection Blog - Inheritance and Access Modifiers","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2019-12-26T06:10:43+00:00","dateModified":"2019-12-26T11:27:46+00:00","description":"A child inherits the property of his parent. He can Acquire new properties or modify the inherited ones.Similarly, inheritance is a feature of OOP.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/inheritance-and-access-modifiers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Inheritance and Access Modifiers"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":449,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/1771"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=1771"}],"version-history":[{"count":16,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/1771\/revisions"}],"predecessor-version":[{"id":1797,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/1771\/revisions\/1797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/1792"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=1771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=1771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=1771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}