<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prerak Dholakia on Medium]]></title>
        <description><![CDATA[Stories by Prerak Dholakia on Medium]]></description>
        <link>https://medium.com/@prerak-dholakia?source=rss-5fd090b77fd0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LvxZYXSZi4OrUVoX.</url>
            <title>Stories by Prerak Dholakia on Medium</title>
            <link>https://medium.com/@prerak-dholakia?source=rss-5fd090b77fd0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:31:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prerak-dholakia/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Object Oriented Programming — What, Why and How]]></title>
            <link>https://prerak-dholakia.medium.com/object-oriented-programming-what-why-and-how-ddfd519c4237?source=rss-5fd090b77fd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddfd519c4237</guid>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Prerak Dholakia]]></dc:creator>
            <pubDate>Mon, 31 May 2021 18:10:16 GMT</pubDate>
            <atom:updated>2021-05-31T18:10:16.174Z</atom:updated>
            <content:encoded><![CDATA[<h3>Object Oriented Programming — What, Why and How</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*tlRI11nhESVLeZczKSh6qQ.png" /><figcaption>source: <a href="https://images.app.goo.gl/fh38TkWEdf7MRmKP9">https://images.app.goo.gl/fh38TkWEdf7MRmKP9</a></figcaption></figure><p>OOP is a programming paradigm in which a developer solves a given development task by relating it with real life entities or objects. As any real life entity, these objects can have memory(store knowledge/state/data) and can have certain functionality (methods).<br>OOP is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.</p><p>Before diving deeper, let’s understand some basic terminologies:</p><p><strong>1. Class<br></strong>It is a template or generic blueprint about the capability of what an object can do. It can be used to create specific, concrete objects.</p><p><strong>2. Attribute<br></strong>An attribute is a field or information which a class holds. It defines the type of field an instance of the class can have but does not contain actual value. The values of these fields are assigned/updated either upon instantiation of objects or while executing any instance methods.<br>These values can define the state of the object and allow it to perform various methods differently based on its value.</p><p>Once the object is instantiated, these attributes contain actual values and are called instance variables.</p><p><strong>3. Methods<br></strong>Methods define behavior of a class. These are defined in the class/blueprint of an object. And once the object is instantiated, these methods can be used to perform specific behavior and update attribute values if required. Methods can also be used to abstract certain logic in order to create reusable code. Such abstraction and reusability allows for better maintenance of the code as any change in the logic of the behavior can be done at a single place and it can reflect at all places where the method is used.</p><p><strong>4. Objects<br></strong>Objects are the instances of the class created with specific methods. Once an object is created, they can interact with other objects and call their methods to update the state of the software program.</p><p><strong>5. Instances<br></strong>There is a very minor difference between objects and instances. Objects are things in memory while instances are things which hold reference to the objects. But in general, both terms can be used interchangeably. See below image for explanation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*5DlKxp4EWl39DrRK3msvgQ.png" /><figcaption>Source: <a href="https://miro.medium.com/max/1400/1*9aK2AU3vlIz8KEHciGsNSg.png">https://miro.medium.com/max/1400/1*9aK2AU3vlIz8KEHciGsNSg.png</a></figcaption></figure><p>Lets understand the above terminologies using following example of a Dog and Animal class</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f7d810ec0d8db69634d0c6eb9821f9f/href">https://medium.com/media/6f7d810ec0d8db69634d0c6eb9821f9f/href</a></iframe><p>As can be seen above, Dog class is a generic blueprint and two specific Dog objects, dog1 and dog2 are created(or instantiated).</p><p>These dogs have attributes like <strong>breed</strong>, <strong>name</strong> and <strong>origin</strong> and methods like <strong>move</strong>, <strong>bark</strong>, <strong>isTired</strong> which updates those attributes.</p><h3>Fundamental principles</h3><p>There are mainly four principles in OOP:</p><ul><li><em>Encapsulation</em></li><li><em>Abstraction</em></li><li><em>Polymorphism</em></li><li><em>Inheritance</em></li></ul><p>Lets understand each topic in detail:</p><h4><strong>1. Encapsulation:</strong></h4><p>Encapsulation is a mechanism of wrapping the instance variables and methods together as a single unit like a Class and only exposing selected information to the outside world.</p><p>In order to implement encapsulation, class can have two types of fields</p><p><em>Private fields</em>: Attributes and methods accessible by class itself. <br><em>Public field</em>s: Attributes and methods accessible by other classes.</p><p>This ensures that any change in the private fields can be done through public methods only and other developers do not mistakenly change the important data and thus access to the data and code is tightly controlled by a class itself.</p><p>For example in the above Dog class example, name, breed and origin strings are private fields and the only way to access those fields is by using related public methods only. Also, there is no way to access the <em>stepsMoved</em> field as the developer might not want to expose this method to the external world and use it for maintaining internal state only.</p><p>Apart from public and private fields there can also be one other type of accessibility layer known as <strong>protected</strong>. These types of fields are accessible with class as well as other class in its inheritance hierarchy.</p><h4>2. Abstraction:</h4><p>Abstraction is a process of hiding the implementation details from the user and only providing functionality to the user.</p><p>In other words, the user will have the information on what the object does instead of how it does it.</p><p>For example, the computer can be connected to the internet in many ways like ethernet, wifi, dial up modem etc.) however, the web browser does not need to know about how it is connected to the internet. It only needs to know if it is connected to the internet or not. Here the lower-level software in the computer provides a common abstraction that the browser can rely on. So “connection to the network” is the abstraction, and Ethernet and Wi-Fi and so on are implementations of that abstraction.<br>`</p><p>Similarly, in our Dog example, to know whether a dog is tired, we can simply call the <em>isTired</em> method and do not need to know the implementation of how a dog is tired.</p><p>Abstracting out functionality through proper interface also helps to reuse the code and allows to avoid code duplication.</p><p><strong>Encapsulation vs Abstraction:</strong></p><p>Encapsulation groups together data and methods that act upon the data, while abstraction deals with exposing the interface to the user and hiding the details of implementation.</p><h4>3. Inheritance</h4><p>Inheritance is a way for a class to extend the functionality of another class. Class which extends other the class is called child class or subclass and the the class which is extended is called parent class or super class.<br>All the attributes and methods of a parent class are available to child class and child class can extend those or update the existing fields as per its specific requirements.<br>So, the idea of inheritance implements the <strong>is a</strong> relationship</p><p>Consider the following class named Animal. It implements the <em>move</em> method which increments the <strong>stepsMoved</strong> counter by 1.</p><p>Now, if Dog extends Animal, method move and attribute type is also extended to it. But since the move method for Dog class is different, it overrides the existing move method from Animal and implements the method based on its requirement.</p><p>Types of inheritance:</p><p>1. <strong>Single Inheritance</strong>: A single child extends a single parent class.<br>Eg. Dog extends Animal</p><p>2. <strong>Multi-Level Inheritance</strong>: A class inherits from a parent class and extends it and itself is a parent class of some another child class<br>Eg. Puppy extends Dog and Dog extends Animal</p><p>3. <strong>Hierarchical Inheritance</strong>: Multiple child classes inherit from the same parent class.<br>Eg. Cat extends Animal and Dog extends Animal</p><p>4. <strong>Multiple inheritance</strong>: A class extends from multiple parent classes. This type of inheritance mechanism is not implemented in some programming languages like Java as it can lead to the <a href="https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem">diamond problem</a>.</p><h4>4. Polymorphism:</h4><p>Polymorphism means the ability to have multiple form of a same object. Polymorphism implementation varies among language. In Java there are basically two types of polymorphism:</p><p><strong>Compile time polymorphism: </strong>This is implemented using method overloading. A class can have the same method name having different method signature and output changes based on how it is called.</p><p>For example in the code given below, a1.eat outputs a different string when called using one and two arguments.</p><p><strong>Runtime polymorphism</strong>:<br>It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.</p><p>Method overriding occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.</p><p>For example in the code given below, both a1 and a2 are Animal instances, but since they contain reference to different objects, output of the move method is different based on methods which are overridden by respective classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b59523e5667c08dfbfe2bba63932efd/href">https://medium.com/media/2b59523e5667c08dfbfe2bba63932efd/href</a></iframe><p>I hope this article have helped you in basic understanding of the topic. Please share your feedback and let me know which topic I should chose for my next article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddfd519c4237" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>