{"id":1346,"date":"2023-10-20T14:58:07","date_gmt":"2023-10-20T14:58:07","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1346"},"modified":"2023-10-31T15:04:15","modified_gmt":"2023-10-31T15:04:15","slug":"what-are-the-example-of-inheritance-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/","title":{"rendered":"What are the example of Inheritance laravel"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Inheritance ?<\/strong><\/h2>\n\n\n\n<p>Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (subclass or derived class) to inherit the characteristics and behaviors of an existing class (superclass or base class). This means that the subclass can reuse the code of the superclass, promoting code reusability and establishing a hierarchical relationship between classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are the benefits of Inheritance in laravel ?<\/h2>\n\n\n\n<p>In Laravel, as in any object-oriented programming (OOP) framework, the benefits of inheritance remain consistent. Here are some advantages specific to Laravel:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Code Reusability:<\/strong> Inheritance allows you to reuse code from existing classes in new classes. In Laravel, this means that you can extend and inherit functionality from base classes, reducing the need to duplicate code.<\/li>\n\n\n\n<li><strong>Maintainability:<\/strong> When you make changes to a base class, those changes automatically propagate to all the subclasses that inherit from it. This makes it easier to maintain and update your codebase.<\/li>\n\n\n\n<li><strong>Polymorphism:<\/strong> Inheritance supports polymorphism, which allows objects of different classes to be treated as objects of a common base class. This can be particularly useful in Laravel when dealing with relationships and polymorphic relationships in Eloquent models.<\/li>\n\n\n\n<li><strong>Organized Code Structure:<\/strong> By using inheritance, you can create a clear and organized class hierarchy. This makes your code more readable and understandable, especially for developers who are new to the project.<\/li>\n\n\n\n<li><strong>Framework Extensibility:<\/strong> Laravel itself extensively uses inheritance to provide a flexible and extensible framework. Many of the core components, such as controllers and models, can be extended to customize their behavior according to your application&#8217;s needs.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example :- <\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nclass User extends Model\r\n{\r\n    protected $fillable = &#91;\r\n        'name',\r\n        'email',\r\n        'password',\r\n    ];\r\n\r\n    public function posts()\r\n    {\r\n        return $this->hasMany(Post::class);\r\n    }\r\n}\r\n\r\nclass Admin extends User\r\n{\r\n    \/\/ Add new properties\r\n    protected $fillable = &#91;\r\n        'name',\r\n        'email',\r\n        'password',\r\n        'role',\r\n    ];\r\n\r\n    \/\/ Override the canPost() method from the parent class\r\n    public function canPost()\r\n    {\r\n        return true;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>In this example, the <code class=\"\">Admin<\/code> class inherits from the <code class=\"\">User<\/code> class. This means that the <code class=\"\">Admin<\/code> class has access to all of the properties and methods of the <code class=\"\">User<\/code> class, including the <code class=\"\">fillable<\/code> property and the <code class=\"\">posts()<\/code> method.<\/p>\n\n\n\n<p>The <code class=\"\">Admin<\/code> class also adds new properties and overrides the <code class=\"\">canPost()<\/code> method. This allows the <code class=\"\">Admin<\/code> class to be more specialized for representing administrator data and functionality.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Inheritance ? Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (subclass or derived class) to inherit the characteristics and&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[528,529,3,527],"class_list":["post-1346","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-inheritance","tag-inheritance-in-laravel","tag-laravel","tag-oops"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What are the example of Inheritance laravel - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What are the example of Inheritance laravel - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"What is Inheritance ? Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (subclass or derived class) to inherit the characteristics and...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-20T14:58:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-31T15:04:15+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/\",\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/\",\"name\":\"What are the example of Inheritance laravel - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#website\"},\"datePublished\":\"2023-10-20T14:58:07+00:00\",\"dateModified\":\"2023-10-31T15:04:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.devopsfreelancer.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What are the example of Inheritance laravel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#website\",\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\/\/www.facebook.com\/amitsthakurs\/\",\"https:\/\/www.instagram.com\/amits_thakurs\/\",\"https:\/\/www.linkedin.com\/in\/amits-thakurs\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs\"],\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What are the example of Inheritance laravel - DevOps Freelancer","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:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/","og_locale":"en_US","og_type":"article","og_title":"What are the example of Inheritance laravel - DevOps Freelancer","og_description":"What is Inheritance ? Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (subclass or derived class) to inherit the characteristics and...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-10-20T14:58:07+00:00","article_modified_time":"2023-10-31T15:04:15+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/","name":"What are the example of Inheritance laravel - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-10-20T14:58:07+00:00","dateModified":"2023-10-31T15:04:15+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-inheritance-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What are the example of Inheritance laravel"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1346"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1346\/revisions"}],"predecessor-version":[{"id":1347,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1346\/revisions\/1347"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}