<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Inheritance &#8211; Best DevOps</title>
	<atom:link href="https://www.bestdevops.com/tag/inheritance/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.bestdevops.com</link>
	<description>Lets Learn, Do it &#38; Share! Thats a Best DevOps!!!</description>
	<lastBuildDate>Wed, 01 Jun 2022 06:36:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>
	<item>
		<title>Object Oriented Programming Principles</title>
		<link>https://www.bestdevops.com/object-oriented-programming-principles/</link>
					<comments>https://www.bestdevops.com/object-oriented-programming-principles/#respond</comments>
		
		<dc:creator><![CDATA[anil]]></dc:creator>
		<pubDate>Sat, 30 Apr 2022 08:57:00 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Encapsulation]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[Type of Inheritance in php:]]></category>
		<guid isPermaLink="false">https://www.bestdevops.com/?p=23397</guid>

					<description><![CDATA[Encapsulation: PHP means, enclosing the internal details of the object to protect from external sources. It describes, combining the class, [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Encapsulation:</h2>



<p class="wp-block-paragraph">PHP means, enclosing the internal details of the object to protect from external sources. It describes, combining the class, data variables and member functions that work on data together within a single unit to form an object.</p>



<p class="wp-block-paragraph">PHP program for encapsulation: The methods or functions in the following program are update password and check the course name. user class defines all the operations related to users.</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>
  
<span class="hljs-comment">// PHP program to implements encapsulation</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">user</span> </span>{
  
    <span class="hljs-keyword">private</span> $userId;
    <span class="hljs-keyword">private</span> $pwd;
      
    <span class="hljs-comment">// Update user password</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updatePwd</span><span class="hljs-params">($userId, $pwd)</span> </span>{
          
        <span class="hljs-comment">// Write function body</span>
        <span class="hljs-keyword">echo</span>(<span class="hljs-string">"Function to update password '"</span>
                . $pwd . <span class="hljs-string">"' for user "</span> . $userId);
          
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"&lt;br&gt;"</span>;
    }
  
    <span class="hljs-comment">// Check account balance</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">courseName</span><span class="hljs-params">($userId)</span> </span>{
          
        <span class="hljs-comment">// Write function body</span>
        <span class="hljs-keyword">echo</span>(<span class="hljs-string">"Function to check course name of user "</span>
                . $userId);
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"&lt;br&gt;"</span>;
    }
}
  
$obj = <span class="hljs-keyword">new</span> user();
$obj -&gt; updatePwd(<span class="hljs-string">'user1'</span>, <span class="hljs-string">'abhi2'</span>);
$obj -&gt; courseName(<span class="hljs-string">'PHP'</span>);
  
<span class="hljs-meta">?&gt;</span></span></code></span></pre>


<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>Output:</strong></p><p>Function to update password &#8216;abhi2&#8217; for user user1<br>Function to check course name of user PHP</p></blockquote>



<h3 class="wp-block-heading">Advantage of Encapsulation :</h3>



<ul class="wp-block-list"><li>It reduces human errors.</li><li>It makes easier to understand.</li><li>It&#8217;s protects an object from unwanted access by clients.</li><li>Encapsulation reduces human errors.</li></ul>



<h2 class="wp-block-heading">Inheritance:</h2>



<ul class="wp-block-list"><li>Accessing the features of one class from another class.</li><li>Inheritance allows you to write the code in the parent class and use it in both parent and child classes.</li><li>The parent class is also called a base class or super class. And the child class is also known as a derived class or a subclass.</li></ul>



<h3 class="wp-block-heading">Declaration of Child Class :</h3>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ChildClassName</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ParentClassName</span>
</span>{
members <span class="hljs-keyword">of</span> Child <span class="hljs-class"><span class="hljs-keyword">class</span>
}</span>;</code></span></pre>


<h3 class="wp-block-heading">Type of Inheritance in php:</h3>



<ul class="wp-block-list"><li>Single Inheritance</li><li>Multi-level Inheritance</li><li>Hierarchical Inheritance</li></ul>



<h3 class="wp-block-heading">Single Inheritance:</h3>



<p class="wp-block-paragraph">If a class is derived from one base class (Parent Class), it is called Single Inheritance.</p>



<h3 class="wp-block-heading"><br>Syntax:</h3>


<pre class="wp-block-code"><span><code class="hljs language-javascript">
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
  </span>{ 
	 members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
  }</span>; 

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span> 
 </span>{ 
	 members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> 
 }</span>; 
</code></span></pre>


<h3 class="wp-block-heading">Example of single level Inheritance:</h3>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span>
     </span>{
         <span class="hljs-keyword">public</span> $a;
         <span class="hljs-keyword">public</span> $b;
         <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getvalue</span><span class="hljs-params">($x, $y)</span>
         </span>{
             <span class="hljs-keyword">$this</span>-&gt;a = $x;
             <span class="hljs-keyword">$this</span>-&gt;b = $y;
         } 
     }
     <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span></span>{
       <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">display</span><span class="hljs-params">()</span>
         </span>{
             <span class="hljs-keyword">echo</span><span class="hljs-string">"value of A:$this-&gt;a &lt;br&gt;"</span>;
             <span class="hljs-keyword">echo</span><span class="hljs-string">"value of B:$this-&gt;b &lt;br&gt;"</span>;

        }
     }
     $obj = <span class="hljs-keyword">new</span> son;
     $obj-&gt;getvalue(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>);
     $obj-&gt;display();

    <span class="hljs-meta">?&gt;</span></span> </code></span></pre>


<h3 class="wp-block-heading"><strong>Multi-level Inheritance:</strong></h3>



<p class="wp-block-paragraph">In multi-level inheritance, the class inherits the feature of another derived class (Child Class).</p>



<p class="wp-block-paragraph"><strong>syntax:</strong></p>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
  </span>{ 
 	members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
  }</span>; 

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span> 
  </span>{ 
 	members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> 
  }</span>; 

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GrandSon</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Son</span> 
  </span>{ 
 	members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GrandSon</span> 
  }</span>; </code></span></pre>


<h3 class="wp-block-heading">Example of Multi-level Inheritance:</h3>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span>
     </span>{
         <span class="hljs-keyword">public</span> $a;
         <span class="hljs-keyword">public</span> $b;
         <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getvalue</span><span class="hljs-params">($x, $y)</span>
         </span>{
             <span class="hljs-keyword">$this</span>-&gt;a = $x;
             <span class="hljs-keyword">$this</span>-&gt;b = $y;
         } 
     }
     <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span>
     </span>{
         <span class="hljs-keyword">public</span> $c=<span class="hljs-number">30</span>;
         <span class="hljs-keyword">public</span> $sum;
         <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-params">()</span>
         </span>{
             <span class="hljs-keyword">$this</span>-&gt;sum=<span class="hljs-keyword">$this</span>-&gt;a+<span class="hljs-keyword">$this</span>-&gt;b+<span class="hljs-keyword">$this</span>-&gt;c;
             <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;sum;
         }
     }
         <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">grandson</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">son</span>
         </span>{
            <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">display</span><span class="hljs-params">()</span>
             </span>{
             <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of A :$this-&gt;a &lt;br&gt;"</span>;
             <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of B :$this-&gt;b &lt;br&gt;"</span>;
             <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of C :$this-&gt;c &lt;br&gt;"</span>;
             <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of sum : $this-&gt;sum &lt;br&gt;"</span>;
             }
        }
     $obj = <span class="hljs-keyword">new</span> grandson;
     $obj-&gt;getValue(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>);
     $obj-&gt;add();
     $obj-&gt;display();
<span class="hljs-meta">?&gt;</span></span></code></span></pre>


<h3 class="wp-block-heading">Hierarchical Inheritance:</h3>


<pre class="wp-block-code"><span><code class="hljs language-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
  </span>{ 
 	members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span> 
  }</span>; 

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span> 
  </span>{ 
 	members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Son</span> 
  }</span>; 

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Daughter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span> 
  </span>{ 
 	members <span class="hljs-keyword">of</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Daughter</span> 
  }</span>; 
</code></span></pre>


<h3 class="wp-block-heading">Example of Hierarchical Inheritance:</h3>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>
 <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Father</span>
    </span>{
        <span class="hljs-keyword">public</span> $a;
        <span class="hljs-keyword">public</span> $b;
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getvalue</span><span class="hljs-params">($x, $y)</span>
        </span>{
            <span class="hljs-keyword">$this</span>-&gt;a = $x;
            <span class="hljs-keyword">$this</span>-&gt;b = $y;
        } 
    }
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">son</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span>
    </span>{
        <span class="hljs-keyword">public</span> $c=<span class="hljs-number">30</span>; 
        <span class="hljs-keyword">public</span> $sum;
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-params">()</span>
        </span>{
            <span class="hljs-keyword">$this</span>-&gt;sum=<span class="hljs-keyword">$this</span>-&gt;a+<span class="hljs-keyword">$this</span>-&gt;b+<span class="hljs-keyword">$this</span>-&gt;c;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;sum;
        }
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">display</span><span class="hljs-params">()</span></span>{
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of A:$this-&gt;a &lt;br&gt;"</span>;
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of b:$this-&gt;b &lt;br&gt;"</span>;
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of c:$this-&gt;c &lt;br&gt;"</span>;
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"sum: $this-&gt;sum  &lt;br&gt;"</span>;
        }
    }   
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Daugther</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Father</span>

    </span>{
        <span class="hljs-keyword">public</span> $c=<span class="hljs-number">30</span>;
        <span class="hljs-keyword">public</span> $multi;
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiply</span><span class="hljs-params">()</span>
        </span>{
            <span class="hljs-keyword">$this</span>-&gt;multi=<span class="hljs-keyword">$this</span>-&gt;a*<span class="hljs-keyword">$this</span>-&gt;b*<span class="hljs-keyword">$this</span>-&gt;c;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;multi;
        }
        <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">display</span><span class="hljs-params">()</span></span>{
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of A:$this-&gt;a &lt;br&gt;"</span>;
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of b:$this-&gt;b &lt;br&gt;"</span>;
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"value of c:$this-&gt;c &lt;br&gt;"</span>;
            <span class="hljs-keyword">echo</span> <span class="hljs-string">"multipication: $this-&gt;multi"</span>;
        }
    }  
        $objs = <span class="hljs-keyword">new</span> son;
        $objD = <span class="hljs-keyword">new</span> Daugther;
        $objs-&gt;getValue(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>);
        $objs-&gt;add();
        $objs-&gt;display();
        $objD-&gt;getValue(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>);
        $objD-&gt;multiply();
        $objD-&gt;display();
    <span class="hljs-meta">?&gt;</span></span>    </code></span></pre>


<h2 class="wp-block-heading">polymorphism:</h2>



<p class="wp-block-paragraph">Polymorphism means &#8220;many forms&#8221;, and it occurs when we have many classes that are related to each other by inheritance</p>



<p class="wp-block-paragraph"><strong>Real life example of polymorphism:</strong> A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism.</p>



<h3 class="wp-block-heading">Example of polymorphism:</h3>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">base</span>

</span>{

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-params">($a,$b)</span>

</span>{

$res=$a*$b;

<span class="hljs-keyword">echo</span> <span class="hljs-string">"Multiplication = "</span>.$res;

}

}


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">child</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">base</span>

</span>{

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-params">($a,$b)</span>

</span>{

$res=$a+$b;

<span class="hljs-keyword">echo</span> <span class="hljs-string">"Sum  = "</span>.$res;

}

}

 $obj= <span class="hljs-keyword">new</span> child();

 $obj-&gt;add(<span class="hljs-number">1000</span>,<span class="hljs-number">500</span>);
 
<span class="hljs-meta">?&gt;</span></span></code></span></pre>


<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>Output:</strong><br>Sum = 1500</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/object-oriented-programming-principles/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
