<?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>polymorphism &#8211; Best DevOps</title>
	<atom:link href="https://www.bestdevops.com/tag/polymorphism/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>Fri, 16 May 2025 08:35:36 +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: Fundamentals, Use Cases, Architecture, and Getting Started</title>
		<link>https://www.bestdevops.com/object-oriented-programming-fundamentals-use-cases-architecture-and-getting-started/</link>
					<comments>https://www.bestdevops.com/object-oriented-programming-fundamentals-use-cases-architecture-and-getting-started/#respond</comments>
		
		<dc:creator><![CDATA[vijay1 vijay1]]></dc:creator>
		<pubDate>Fri, 16 May 2025 08:35:32 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Abstraction]]></category>
		<category><![CDATA[Encapsulation]]></category>
		<category><![CDATA[ObjectOrientedProgramming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[SoftwareDevelopment]]></category>
		<guid isPermaLink="false">https://www.bestdevops.com/?p=31173</guid>

					<description><![CDATA[What is Object-Oriented Programming? Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of objects, which bundle data [&#8230;]]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="522" src="https://www.bestdevops.com/wp-content/uploads/2025/05/image-63-1024x522.png" alt="" class="wp-image-31181" srcset="https://www.bestdevops.com/wp-content/uploads/2025/05/image-63-1024x522.png 1024w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-63-300x153.png 300w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-63-768x392.png 768w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-63.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading"><strong>What is Object-Oriented Programming?</strong></h3>



<p class="wp-block-paragraph">Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of <strong>objects</strong>, which bundle data and behavior together. Instead of writing code as a sequence of instructions (procedural programming), OOP models real-world entities as objects that have properties (attributes) and actions (methods).</p>



<p class="wp-block-paragraph">OOP aims to improve code modularity, reusability, scalability, and maintainability by organizing software design around objects and their interactions. Popular OOP languages include Java, C++, Python, C#, Ruby, and many others.</p>



<p class="wp-block-paragraph">Key principles of OOP include:</p>



<ul class="wp-block-list">
<li><strong>Encapsulation:</strong> Wrapping data and methods that manipulate data within objects, hiding internal details.</li>



<li><strong>Inheritance:</strong> Allowing new objects to inherit properties and behavior from existing objects.</li>



<li><strong>Polymorphism:</strong> Enabling objects to be treated as instances of their parent class while exhibiting behavior specific to their actual type.</li>



<li><strong>Abstraction:</strong> Simplifying complex systems by modeling only essential features relevant to the context.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>What Are the Major Use Cases of Object-Oriented Programming?</strong></h3>



<p class="wp-block-paragraph">OOP is widely applicable across many domains, especially where complex software systems require clear structure and extensibility:</p>



<h4 class="wp-block-heading">1. <strong>Software Engineering and Application Development</strong></h4>



<ul class="wp-block-list">
<li>Building desktop, mobile, and web applications with modular and maintainable codebases.</li>



<li>Example: GUI frameworks like Qt and JavaFX.</li>
</ul>



<h4 class="wp-block-heading">2. <strong>Game Development</strong></h4>



<ul class="wp-block-list">
<li>Modeling game characters, environments, and interactions as objects.</li>



<li>Supports polymorphism for AI behaviors and inheritance for character types.</li>
</ul>



<h4 class="wp-block-heading">3. <strong>Enterprise Systems</strong></h4>



<ul class="wp-block-list">
<li>Designing scalable business applications with complex workflows.</li>



<li>Leverages OOP for domain modeling and layered architectures.</li>
</ul>



<h4 class="wp-block-heading">4. <strong>Simulation and Modeling</strong></h4>



<ul class="wp-block-list">
<li>Represents real-world systems such as traffic, ecosystems, or financial markets using interacting objects.</li>
</ul>



<h4 class="wp-block-heading">5. <strong>Framework and Library Development</strong></h4>



<ul class="wp-block-list">
<li>Facilitates reusable software components and APIs.</li>
</ul>



<h4 class="wp-block-heading">6. <strong>Machine Learning and AI</strong></h4>



<ul class="wp-block-list">
<li>Implements algorithms and data pipelines as objects for flexibility and modularity.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>How Object-Oriented Programming Works Along with Architecture</strong></h3>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="512" src="https://www.bestdevops.com/wp-content/uploads/2025/05/image-64-1024x512.png" alt="" class="wp-image-31182" srcset="https://www.bestdevops.com/wp-content/uploads/2025/05/image-64-1024x512.png 1024w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-64-300x150.png 300w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-64-768x384.png 768w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-64.png 1280w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p class="wp-block-paragraph">OOP is not just a programming style but also influences software architecture by structuring programs into interacting components.</p>



<h4 class="wp-block-heading"><strong>Key Architectural Elements:</strong></h4>



<ol class="wp-block-list">
<li><strong>Classes and Objects</strong>
<ul class="wp-block-list">
<li><strong>Class:</strong> Blueprint defining properties and behaviors.</li>



<li><strong>Object:</strong> Concrete instance of a class with specific state.</li>
</ul>
</li>



<li><strong>Encapsulation</strong>
<ul class="wp-block-list">
<li>Data hiding restricts direct access to object internals.</li>



<li>Interfaces expose only what is necessary.</li>
</ul>
</li>



<li><strong>Inheritance Hierarchies</strong>
<ul class="wp-block-list">
<li>Classes are organized into hierarchies.</li>



<li>Subclasses inherit and override parent class methods.</li>
</ul>
</li>



<li><strong>Polymorphism and Dynamic Dispatch</strong>
<ul class="wp-block-list">
<li>Allows method calls to be resolved at runtime based on the object’s actual class.</li>



<li>Supports flexible and interchangeable components.</li>
</ul>
</li>



<li><strong>Design Patterns</strong>
<ul class="wp-block-list">
<li>Reusable solutions like Singleton, Factory, Observer leverage OOP concepts to solve common design challenges.</li>
</ul>
</li>



<li><strong>Object Interaction</strong>
<ul class="wp-block-list">
<li>Objects collaborate by sending messages (calling methods).</li>



<li>Enables distributed, event-driven, or layered architectures.</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>What Are the Basic Workflows of Object-Oriented Programming?</strong></h3>



<p class="wp-block-paragraph">Implementing OOP follows a general workflow:</p>



<ol class="wp-block-list">
<li><strong>Identify Objects</strong>
<ul class="wp-block-list">
<li>Analyze the problem domain to find entities that can be modeled as objects.</li>
</ul>
</li>



<li><strong>Define Classes</strong>
<ul class="wp-block-list">
<li>Specify classes with attributes and methods representing these entities.</li>
</ul>
</li>



<li><strong>Establish Relationships</strong>
<ul class="wp-block-list">
<li>Determine inheritance, associations, and dependencies between classes.</li>
</ul>
</li>



<li><strong>Create Objects</strong>
<ul class="wp-block-list">
<li>Instantiate objects from classes during program execution.</li>
</ul>
</li>



<li><strong>Implement Behavior</strong>
<ul class="wp-block-list">
<li>Write methods encapsulating business logic and interactions.</li>
</ul>
</li>



<li><strong>Use Polymorphism and Abstraction</strong>
<ul class="wp-block-list">
<li>Design flexible and extensible interfaces.</li>
</ul>
</li>



<li><strong>Test and Refine</strong>
<ul class="wp-block-list">
<li>Validate objects’ interactions and behaviors.</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><strong>Step-by-Step Getting Started Guide for Object-Oriented Programming</strong></h3>



<h4 class="wp-block-heading"><strong>Step 1: Choose an OOP Language</strong></h4>



<ul class="wp-block-list">
<li>Select a language like Python, Java, C++, or C# that supports OOP.</li>
</ul>



<h4 class="wp-block-heading"><strong>Step 2: Understand Basic Syntax</strong></h4>



<ul class="wp-block-list">
<li>Learn how to define classes and create objects.</li>



<li>Example in Python: <code>class Animal: def __init__(self, name): self.name = name def speak(self): print(f"{self.name} makes a sound") dog = Animal("Dog") dog.speak()</code></li>
</ul>



<h4 class="wp-block-heading"><strong>Step 3: Learn Core Principles</strong></h4>



<ul class="wp-block-list">
<li>Practice encapsulation by using private/protected members.</li>



<li>Implement inheritance: <code>class Dog(Animal): def speak(self): print(f"{self.name} barks") dog = Dog("Buddy") dog.speak()</code></li>
</ul>



<h4 class="wp-block-heading"><strong>Step 4: Explore Polymorphism</strong></h4>



<ul class="wp-block-list">
<li>Use base class references to call subclass methods: <code>def make_it_speak(animal): animal.speak() make_it_speak(Animal("Generic")) make_it_speak(Dog("Buddy"))</code></li>
</ul>



<h4 class="wp-block-heading"><strong>Step 5: Work with Abstraction</strong></h4>



<ul class="wp-block-list">
<li>Use abstract base classes or interfaces to define contracts.</li>



<li>In Python: <code>from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass</code></li>
</ul>



<h4 class="wp-block-heading"><strong>Step 6: Apply OOP to Small Projects</strong></h4>



<ul class="wp-block-list">
<li>Model real-world problems like a bank system, library management, or simple game.</li>
</ul>



<h4 class="wp-block-heading"><strong>Step 7: Use UML Diagrams</strong></h4>



<ul class="wp-block-list">
<li>Visualize classes and relationships using Unified Modeling Language.</li>
</ul>



<h4 class="wp-block-heading"><strong>Step 8: Explore Design Patterns</strong></h4>



<ul class="wp-block-list">
<li>Learn common OOP design patterns to solve recurring design issues.</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/object-oriented-programming-fundamentals-use-cases-architecture-and-getting-started/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
