<?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: Satya Prakash Nandy</title>
    <description>The latest articles on DEV Community by Satya Prakash Nandy (@satya190597).</description>
    <link>https://dev.to/satya190597</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%2F603022%2F1b015d06-0069-4fa1-b6d3-bc1f54b7a313.jpeg</url>
      <title>DEV Community: Satya Prakash Nandy</title>
      <link>https://dev.to/satya190597</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/satya190597"/>
    <language>en</language>
    <item>
      <title>Association, Composition and Aggregation in Java</title>
      <dc:creator>Satya Prakash Nandy</dc:creator>
      <pubDate>Wed, 05 Jul 2023 07:20:59 +0000</pubDate>
      <link>https://dev.to/satya190597/association-composition-and-aggregation-in-java-2hk8</link>
      <guid>https://dev.to/satya190597/association-composition-and-aggregation-in-java-2hk8</guid>
      <description>&lt;p&gt;In Object Oriented Programming, there can be broadly two types of relationships &lt;strong&gt;"IS-A"&lt;/strong&gt; and &lt;strong&gt;"HAS-A"&lt;/strong&gt; relationships.&lt;br&gt;
We can achieve an &lt;strong&gt;"IS-A"&lt;/strong&gt; relationship using &lt;strong&gt;Inheritance&lt;/strong&gt;.&lt;br&gt;
Whereas we can achieve a &lt;strong&gt;"HAS-A"&lt;/strong&gt; relationship using &lt;strong&gt;Association&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composition&lt;/strong&gt; and &lt;strong&gt;Aggregation&lt;/strong&gt; are two forms of Association.&lt;/p&gt;
&lt;h2&gt;
  
  
  Association.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Association&lt;/strong&gt; represents a relationship between two separate classes where objects of one class are related to objects of another class. It is a &lt;strong&gt;loosely&lt;/strong&gt; coupled relationship, meaning that the &lt;strong&gt;associated objects&lt;/strong&gt; can exist &lt;strong&gt;independently&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The association can be one-to-one, one-to-many, many-to-one or many-to-many.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Let's take an example of the Car and Engine class. In this example, Both car and engine classes can exist independently. The lifecycle of the Engine class object does not depend on the Car class object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }
}

class Engine {
    // Engine class implementation
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Composition.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Composition&lt;/strong&gt; represents a strong form of association where on class is composed of one or more object of another class. The &lt;strong&gt;composed object&lt;/strong&gt; cannot exist &lt;strong&gt;without the existence&lt;/strong&gt; of &lt;strong&gt;parent&lt;/strong&gt; class.&lt;/p&gt;

&lt;p&gt;When the parent object is &lt;strong&gt;destroyed&lt;/strong&gt; all the composed objects are also destroyed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example.
&lt;/h3&gt;

&lt;p&gt;In this example, the life cycle of the Engine object is completely dependent on the Car object. Once the Car object is destroyed, the associated Engine object will also be destroyed. &lt;br&gt;
In this example, instead of passing an Engine object through the constructor, we are creating an Engine object inside the Car constructor. Hence, Engine and Car objects are tightly coupled with each other.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    private Engine engine;

    public Car() {
        this.engine = new Engine();
    }
}

class Engine {
    // Engine class implementation
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Aggregation.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Aggregation&lt;/strong&gt; is a specialized form of &lt;strong&gt;association&lt;/strong&gt; where one class has a reference to another class, but the object can exist &lt;strong&gt;independently&lt;/strong&gt;. It represents a &lt;strong&gt;“has-a”&lt;/strong&gt; relationship, where the associated object can be shared among &lt;strong&gt;multiple objects&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;In this example, Department class contains a list of employee objects.&lt;br&gt;
Each employee object can exist independently and their life cycle does not depend on the Employee object. Also multiple departments can share the same Employee object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Department {
    private List&amp;lt;Employee&amp;gt; employees;

    public Department() {
        employees = new ArrayList&amp;lt;&amp;gt;();
    }

    public void addEmployee(Employee employee) {
        employees.add(employee);
    }
}

class Employee {
    // Employee class implementation
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Aggregation and association are two forms of composition, where aggregation defines a &lt;strong&gt;'HAS-A'&lt;/strong&gt; relationship with a &lt;strong&gt;weak association&lt;/strong&gt;, while composition represents a &lt;strong&gt;'BELONGS-TO'&lt;/strong&gt; relationship with a &lt;strong&gt;strong association&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why do we need access modifiers in Java? And How does it work?</title>
      <dc:creator>Satya Prakash Nandy</dc:creator>
      <pubDate>Sat, 03 Jun 2023 11:55:49 +0000</pubDate>
      <link>https://dev.to/satya190597/why-do-we-need-access-modifiers-in-java-and-how-does-it-work-394p</link>
      <guid>https://dev.to/satya190597/why-do-we-need-access-modifiers-in-java-and-how-does-it-work-394p</guid>
      <description>&lt;p&gt;Access modifiers are keywords used in programming languages, including Java, to control the visibility and accessibility of class members, such as variables and methods. They allow developers to specify the level of restriction placed on these members and determine how they can be accessed by other developers or code.&lt;br&gt;
In Java, there are four types of access modifiers: &lt;strong&gt;public&lt;/strong&gt;, &lt;strong&gt;private&lt;/strong&gt;, &lt;strong&gt;protected&lt;/strong&gt;, and &lt;strong&gt;default&lt;/strong&gt; (also known as package-private). Each modifier serves a specific purpose in managing access to class members.&lt;/p&gt;
&lt;h2&gt;
  
  
  To better understand this, let's look at some examples.
&lt;/h2&gt;

&lt;p&gt;As a person, there are some topics or incidents that you don't want to share or discuss with anyone. &lt;br&gt;
You can mark them as &lt;strong&gt;private&lt;/strong&gt;.&lt;br&gt;
For example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The day you accidentally deleted some user's data from the production database 💻.&lt;/li&gt;
&lt;li&gt;Or your childhood crush - Jennifer Connelly ❤️.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2G3kc7E---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxlmzks68dh55y7o0h4w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2G3kc7E---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxlmzks68dh55y7o0h4w.gif" alt="Private" width="498" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now there are some pieces of information about your life that you willingly share with everyone. &lt;br&gt;
You can mark them as &lt;strong&gt;public&lt;/strong&gt;.&lt;br&gt;
For example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your name and Snapchat ID.&lt;/li&gt;
&lt;li&gt;Or your selfie with your latest iPhone 📱. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_Kb4za2U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nquv8seyo4k50j2xdp0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_Kb4za2U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nquv8seyo4k50j2xdp0.gif" alt="Iphone" width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's say that you are a member of the Flat Earth Society 🌍. You cannot discuss some topics with anyone else except other members of that group.&lt;br&gt;
You can label them as &lt;strong&gt;default&lt;/strong&gt;.&lt;br&gt;
For example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You found a new mathematical formula ✏️ to prove that the Earth is flat.&lt;/li&gt;
&lt;li&gt;Or You visited the North Pole and found a huge wall on the edge ❄️.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zyO2l8W0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kyisfd8tyx6llc3l1tq7.gif" alt="Flat Earth" width="498" height="375"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can only share these things with people who belong to that group. &lt;/p&gt;

&lt;p&gt;Finally, let's talk about the &lt;strong&gt;protected&lt;/strong&gt; access modifier. Let us say there is something that you can share with your family members, your children, and your grandchildren. &lt;br&gt;
For example, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can talk about how access modifiers work 📚. &lt;/li&gt;
&lt;li&gt;Or you can just brag about how you have played through all the Dark Souls games ⚔️.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VabWt0kq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jutjpnv6jwptu65nwkpf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VabWt0kq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jutjpnv6jwptu65nwkpf.gif" alt="Dark Soul" width="498" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In programming, access modifiers work similarly. They allow you to control the visibility and accessibility of class members, just like you control what you share privately, publicly, within specific groups, or with specific individuals. By using access modifiers appropriately, you can ensure the right level of privacy, security, and encapsulation in your code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's look at some code examples
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;File structure.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pNQRyYF9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwuiq3cadwa3mv2h53ya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pNQRyYF9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwuiq3cadwa3mv2h53ya.png" alt="Project Structure" width="288" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have three different packages &lt;strong&gt;FlatEarthSociety&lt;/strong&gt;, &lt;strong&gt;Family&lt;/strong&gt;, and &lt;strong&gt;Office&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;strong&gt;FlatEarthSociety&lt;/strong&gt; package, we have two classes, &lt;strong&gt;Vickie&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;strong&gt;Family&lt;/strong&gt; package, we have one class &lt;strong&gt;MaxJunior&lt;/strong&gt; derived from Max.&lt;/li&gt;
&lt;li&gt;And In the &lt;strong&gt;Office&lt;/strong&gt; package, we have one class, &lt;strong&gt;Thomas&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Max.class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Max {
    public String name = "Max";
    public String latestIphone = "iPhone 14 Pro Max";
    private String childhoodCrush = "Jennifer Connelly";
    boolean iBelieveInFlatEarth = true; // DEFAULT
    protected int numberOfDarkSoulGamesPlayed = 8;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Thomas.class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Thomas {
    public void display() {
        Max max = new Max();
        System.out.println(max.name);
        System.out.println(max.latestIphone);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Thomas can access only &lt;strong&gt;"name"&lt;/strong&gt; and &lt;strong&gt;"latestIphone"&lt;/strong&gt; data members.&lt;br&gt;
As Thomas belongs to a different package, he can only access the &lt;strong&gt;public&lt;/strong&gt; members of Max.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Vickie.class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Vickie {
    public void display() {
        Max max = new Max();
        System.out.println(max.name);
        System.out.println(max.latestIphone);
        System.out.println(max.iBelieveInFlatEarth);
        System.out.println(max.numberOfDarkSoulGamesPlayed);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Vickie can access &lt;strong&gt;"name"&lt;/strong&gt;, &lt;strong&gt;"latestIphone"&lt;/strong&gt;, &lt;strong&gt;"iBelieveInFlatEarth"&lt;/strong&gt; and &lt;strong&gt;"numberOfDarkSoulGamesPlayed"&lt;/strong&gt; data members.&lt;br&gt;
As Vickie belongs to the same package, he can access &lt;strong&gt;public&lt;/strong&gt;, &lt;strong&gt;protected&lt;/strong&gt;, and &lt;strong&gt;default&lt;/strong&gt; data members. But cannot access private data members.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;MaxJunior.class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MaxJunior extends Max {
    public void display() {
       MaxJunior grandpa = new MaxJunior();
       System.out.println(grandpa.latestIphone);
       System.out.println(grandpa.name);
       System.out.println(grandpa.numberOfDarkSoulGamesPlayed);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Despite being present in a different package, MaxJunior can access &lt;strong&gt;public&lt;/strong&gt; and &lt;strong&gt;protected&lt;/strong&gt; data members of Max class.&lt;br&gt;
This is because it inherits the Max class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Last but not least, No one can access Max's &lt;strong&gt;childhoodCrush&lt;/strong&gt; ❤️ except for Max because it's private! &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary Table 📎.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wv6RBzCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hn7plsvmbos46fyqwgb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wv6RBzCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hn7plsvmbos46fyqwgb3.png" alt="Access Modifiers" width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
