<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Moises Gamio</title>
    <description>The latest articles on DEV Community by Moises Gamio (@moisesgamio).</description>
    <link>https://dev.to/moisesgamio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1175924%2F3d93be9e-d8b2-4cb4-b82a-95eebae7fc4c.jpg</url>
      <title>DEV Community: Moises Gamio</title>
      <link>https://dev.to/moisesgamio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moisesgamio"/>
    <language>en</language>
    <item>
      <title>Understanding OOP concepts</title>
      <dc:creator>Moises Gamio</dc:creator>
      <pubDate>Sat, 03 Feb 2024 17:53:41 +0000</pubDate>
      <link>https://dev.to/moisesgamio/understanding-oop-concepts-42a8</link>
      <guid>https://dev.to/moisesgamio/understanding-oop-concepts-42a8</guid>
      <description>&lt;p&gt;Understanding OOP concepts gives you a solid foundation for making critical decisions about object-oriented software design.&lt;/p&gt;

&lt;h3&gt;
  
  
  Class
&lt;/h3&gt;

&lt;p&gt;A class is a template or prototype that describes what an object will be. It defines its attributes(data) and behavior(methods). We must design a class before creating an object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object
&lt;/h2&gt;

&lt;p&gt;An object is an instance of a class. When we create an object, we create real-world entities such as cars, bicycles, or dogs with their own attributes and own behaviors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FuVqkoHD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://codersite.dev/assets/images/carClass.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FuVqkoHD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://codersite.dev/assets/images/carClass.jpg" alt="carClass" width="730" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We instantiate an object via the &lt;em&gt;new&lt;/em&gt; keyword in the Java programming language. When you design a class follow the &lt;a href="https://codersite.dev/solid-principles-the-definitive-guide/"&gt;Single Responsibility Principle (SRP)&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstraction in OOP
&lt;/h2&gt;

&lt;p&gt;Abstraction allows an object telling to its users what an application does instead of how it does it. You can see the essential buttons on your TV remote control, but you don't care what happens behind when you press one of these buttons. In Java, we create abstractions via Interfaces and Abstract classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation in OOP and Data Hiding
&lt;/h2&gt;

&lt;p&gt;Restricting access to specific attributes and methods is called &lt;em&gt;data hiding&lt;/em&gt;. Objects should not manipulate the data of other objects. &lt;/p&gt;

&lt;p&gt;Encapsulation is the action of combining the data and methods in the same entity. In this way, we control access to the data in the object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance in OOP
&lt;/h2&gt;

&lt;p&gt;Inheritance is a process in which a class inherits the attributes and methods of another class.&lt;/p&gt;

&lt;p&gt;Class inheritance in OOP provides the ability to create new classes with new functionalities while maintaining the functionalities inherited. In this way, it promotes code reusability.&lt;/p&gt;

&lt;p&gt;This relationship is an &lt;em&gt;is-a&lt;/em&gt; relationship because when a subclass inherits from a superclass, it can do anything that the superclass can do. &lt;/p&gt;

&lt;p&gt;In Java, we create inheritance between classes via the &lt;em&gt;extends&lt;/em&gt; keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphism in OOP
&lt;/h2&gt;

&lt;p&gt;Polymorphism in OOP means many shapes and is coupled to inheritance. For example, a Shape class defines a &lt;em&gt;draw&lt;/em&gt; method, but Square and Circle's subclasses will implement it differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Composition in OOP
&lt;/h2&gt;

&lt;p&gt;The composition in OOP provides a mechanism for building classes from other classes. In Java, we usually create a class with instance variables that references one or more objects of other classes.&lt;/p&gt;

&lt;p&gt;The benefit of separating one class from another one is the &lt;em&gt;Reuse&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example, in a shopping context, we need a list of requested &lt;em&gt;items&lt;/em&gt; and an &lt;em&gt;address&lt;/em&gt; where to deliver the &lt;em&gt;order&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;orderItems&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt; &lt;span class="n"&gt;shippingAddress&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//code omitted for brevity&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We build an &lt;em&gt;Item&lt;/em&gt; class including an &lt;em&gt;Article&lt;/em&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Article&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//code omitted for brevity&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the &lt;em&gt;Article&lt;/em&gt; class includes enough attributes to support the shopping business.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Article&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;double&lt;/span&gt; &lt;span class="n"&gt;deliveryPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//code omitted for brevity&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even we can reuse the &lt;em&gt;Article&lt;/em&gt; class to support a &lt;em&gt;Search&lt;/em&gt; request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//code omitted for brevity&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens if the business wants to introduce articles in a country where some articles are forbidden to trade?.&lt;/p&gt;

&lt;p&gt;We can not add a new attribute called &lt;em&gt;tradable&lt;/em&gt; to the Article class because we will never use it in normal countries.&lt;/p&gt;

&lt;p&gt;Here, we can use the other technique to build new classes: &lt;strong&gt;inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8OWryzOP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://codersite.dev/assets/images/tradableArticle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8OWryzOP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://codersite.dev/assets/images/tradableArticle.png" alt="class and object" width="168" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can support the new requirement for the new country.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;TradableArticle&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//code omitted for brevity&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can reuse our classes even out of context. For example, in a Bank context, we need an &lt;em&gt;address&lt;/em&gt; to contact a &lt;em&gt;customer&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//code omitted for brevity&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the term &lt;em&gt;has-a&lt;/em&gt; to describe composition relationships. An order &lt;em&gt;has-a(n)&lt;/em&gt; address. A customer &lt;em&gt;has-a(n)&lt;/em&gt; address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of object-oriented programming
&lt;/h2&gt;

&lt;p&gt;Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to structure code. It offers several advantages, which have contributed to its popularity and widespread use in software development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Focuses on data: You create a program using objects focused on data. You don't define global data. Every object contains its data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modularity: OOP promotes modularity by encapsulating objects and their behaviors within classes. This allows developers to break down a complex system into smaller, manageable parts, making it easier to understand, maintain, and debug.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reusability: OOP encourages code reusability through inheritance and polymorphism. Developers can create new classes by inheriting properties and behaviors from existing classes, reducing redundancy and promoting a "write once, use many times" approach.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flexibility and Extensibility: OOP allows for easy modification and extension of existing code. You can add new classes and methods without altering the existing codebase, reducing the risk of introducing bugs in previously working code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Abstraction: Abstraction is a key concept in OOP, where you model real-world entities as objects and focus on essential properties and behaviors while hiding unnecessary details. This simplifies problem-solving and enhances code clarity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encapsulation: OOP supports encapsulation by bundling data (attributes) and functions (methods) into objects. Access to an object's internal state is controlled through well-defined interfaces, promoting data integrity and security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Organization: OOP provides a natural way to organize code, making it more intuitive for developers to work collaboratively on large projects. Classes and objects mirror the structure of the problem domain, making it easier to map real-world concepts to code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Maintainability: OOP code tends to be more maintainable because of its modular and organized nature. Changes and updates can be made to specific classes or objects without affecting the entire system, reducing the risk of introducing unintended side effects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testability: OOP code is often easier to test since objects can be isolated and tested independently, leading to more comprehensive and efficient testing strategies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://codersite.dev/clean-code/"&gt;Code Understandability&lt;/a&gt;: OOP promotes a closer alignment between code and real-world concepts, making the codebase more understandable to developers, even those who didn't write the original code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Support for Large-Scale Development: OOP is well-suited for large-scale software development projects. Its inherent structure and organization facilitate teamwork, reduce development time, and enhance project manageability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Community and Ecosystem: OOP has a vast and mature ecosystem of libraries, frameworks, and tools, making it easier to find resources and solutions for common programming tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cross-Disciplinary Applications: OOP applies to various domains and industries, making it a versatile paradigm suitable for a wide range of software development projects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding OOP concepts in Java or Python OOP concepts makes your system design resilient to future changes.&lt;/p&gt;

&lt;p&gt;While OOP offers numerous advantages, it's important to note that it may not always be the best choice for every project or problem. The choice of programming paradigm should align with the specific requirements, constraints, and goals of the software being developed.&lt;/p&gt;

&lt;p&gt;Learn how to use these concepts in &lt;a href="https://codersite.dev/open-closed-principle/"&gt;SOLID design principles&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any software design is generally a matter of opinion. There is no definitive Guide. -- &lt;cite&gt;codersite.dev&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thank you for reading my blog. Feel free to connect on &lt;a href="https://twitter.com/MoisesGamio"&gt;twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ko-fi.com/M4M4UE9UD"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PxysHD6L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://storage.ko-fi.com/cdn/kofi3.png%3Fv%3D3" alt="Buy Me a Coffee at ko-fi.com" width="286" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>softwaredevelopment</category>
      <category>java</category>
    </item>
  </channel>
</rss>
