{"id":78940,"date":"2020-06-03T10:00:56","date_gmt":"2020-06-03T04:30:56","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78940"},"modified":"2020-06-03T10:00:56","modified_gmt":"2020-06-03T04:30:56","slug":"java-association","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-association\/","title":{"rendered":"Java Association &#8211; Aggregation and Composition in Java"},"content":{"rendered":"<p>What if there are two classes that have some kind of relationship between them?<\/p>\n<p>For example, the classes are Library and Books or Teacher and Student, don\u2019t you think these two classes may be associated or connected with each other.<\/p>\n<p>We can establish a relationship between them using Java <strong>Association<\/strong>. This relationship can be any type like one to one, one to many, many to one, or many to many.<\/p>\n<p>Actually, these separate <strong>classes<\/strong> are connected with the help of their <strong>objects<\/strong>. In this article, we will discuss <strong>Association in Java<\/strong>, along with its types and implementation examples in Java.<\/p>\n<p>We will learn each topic with a real-life example for your better understanding.<\/p>\n<h3>What is Association in Java?<\/h3>\n<p>Association in Java is one of the building blocks and the most basic concept of object-oriented programming. Association is a connection or relationship between two separate classes.<\/p>\n<p>It shows how objects of two classes are associated with each other. The Association defines the multiplicity between objects. We can describe the Association as a has-a relationship between the classes.<\/p>\n<p>Association is a kind of relationship between classes whose objects have an independent lifecycle and there is no ownership between the objects. It can be one-to-one, one-to-many, many-to-one, many-to-many.<\/p>\n<p>Let\u2019s take a real-life example to understand these types:<\/p>\n<h4>1. One-to-one<\/h4>\n<p>The best example of a one-to-one association is that one person or one individual can have only one passport. This is a one-to-one relationship between the person and the passport.<\/p>\n<h4>2. One-to-many<\/h4>\n<p>Suppose, there is a Doctor and his patients. So, one doctor is associated with many patients. So this is an example of a one-to-many Association between a doctor and patients.<\/p>\n<h4>3. Many-to-one<\/h4>\n<p>For example, there can be many books in one library, each book is associated with that library, and it can\u2019t be a part of another library. So, many books are related to one library.<\/p>\n<p>This is an example of a many-to-one Association between books and a library.<\/p>\n<h4>4. Many-to-many<\/h4>\n<p>If we talk about a teacher and student, there can be many students associated with one teacher, and also, the teacher can be related to many students.<\/p>\n<p>So, the relationship between a teacher and student can be many-to-many.<\/p>\n<h2><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Association-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78978\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Association-in-Java.jpg\" alt=\"Association in Java\" width=\"752\" height=\"328\" \/><\/a><\/h2>\n<h3>Types of Java Association<\/h3>\n<p>There are two special forms of Association in Java. They are:<br \/>\n<strong>1.<\/strong> Composition<br \/>\n<strong>2.<\/strong> Aggregation<\/p>\n<h4>UML Notations of Association, Composition, and Aggregation in Java<\/h4>\n<p>There are different UML notations for aggregation, associations, and composition. We can easily differentiate them from these notations.<\/p>\n<p>The following diagram shows the UML notations for each of them.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/UML-Notations.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78979\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/UML-Notations.jpg\" alt=\"Java UML Notations\" width=\"425\" height=\"316\" \/><\/a><\/p>\n<p><strong>Example of Association in Java<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.association;\nclass Person {\n  String name;\n  long id;\n  Person(String name, long id) {\n    this.name = name;\n    this.id = id;\n  }\n}\nclass Passport extends Person {\n  String personName;\n  Passport(String name, long id) {\n    super(name, id);\n    this.personName = name;\n  }\n}\npublic class GovernmentAgency {\n  public static void main(String args[]) {\n    Passport obj = new Passport(\"Divya\", 99884444);\n    System.out.println(obj.personName + \" is a person with a passport number: \" + obj.id);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nDivya is a person with a passport number: 99884444<\/p>\n<p>In the above example, there is a one to one association between two classes: <strong>Person<\/strong> and <strong>Passport<\/strong>. Both the classes represent two separate entities.<\/p>\n<h4>Aggregation in Java<\/h4>\n<p>Aggregation in Java is a special kind of association. It represents the Has-A relationship between classes. Java Aggregation allows only one-to-one relationships.<\/p>\n<p>If an object is destroyed, it will not affect the other object, i.e., both objects can work independently.<\/p>\n<p>Let\u2019s take an example. There is an Employee in a company who belongs to a particular Department. If the <strong>Employee<\/strong> object gets destroyed still the <strong>Department<\/strong> can work independently.<\/p>\n<p>The end of the Employee object will not affect or destroy the Department object. The Aggregation is represented as a line with a diamond.<\/p>\n<p>Here the Department has-a Employee and therefore both are in the Aggregation with each other.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Aggregation-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78981\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Aggregation-in-Java.jpg\" alt=\"Aggregation in Java\" width=\"623\" height=\"160\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.association;\nclass Employee {\n  int id;\n  String name;\n  String dept;\n  Employee(int id, String name, String dept) {\n    this.id = id;\n    this.name = name;\n    this.dept = dept;\n    System.out.println(\"\\nEmployee name is \" + name);\n    System.out.println(\"Employee Id is \" + id);\n    System.out.println(\"Employee belongs to the \" + dept + \" Department\");\n  }\n}\nclass Department {\n  String deptName;\n  int noOfemployees;\n  Department(String name, int numberOfemployees) {\n    this.deptName = name;\n    this.noOfemployees = numberOfemployees;\n  }\n}\nclass University {\n  String universityName;\n  int noOfdepartments;\n  University(String name, int departments) {\n    this.universityName = name;\n    this.noOfdepartments = departments;\n  }\n}\npublic class AggregationDemo {\n  public static void main(String[] args) {\n    Employee e1 = new Employee(101, \"Rishi\", \"Engineering\");\n    Employee e2 = new Employee(167, \"Rohan\", \"Management\");\n    Employee e3 = new Employee(125, \"Sneha\", \"Accounts\");\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nEmployee name is Rishi<br \/>\nEmployee Id is 101<br \/>\nEmployee belongs to the Engineering Department<\/p>\n<p>Employee name is Rohan<br \/>\nEmployee Id is 167<br \/>\nEmployee belongs to the Management Department<\/p>\n<p>Employee name is Sneha<br \/>\nEmployee Id is 125<br \/>\nEmployee belongs to the Accounts Department<\/p>\n<h4>Composition in Java<\/h4>\n<p>The composition is another form of aggregation which is considered as the restricted form of Association.<\/p>\n<p>In this type of association, the entities are completely dependent on each other, unlike the aggregation. Composition allows for one-to-many relationships between objects.<\/p>\n<p>It represents a part-of relationship between two objects. One entity cannot exist without the other. Composition in Java represents a one-to-many relationship.<\/p>\n<p>Suppose, there is a <strong>House<\/strong> and inside the house, there are many <strong>rooms<\/strong>. We consider the relationship between the house and the rooms.<\/p>\n<p>A single house can have multiple rooms but a single room can not have multiple houses. And, if we delete the house, the rooms will automatically be deleted.<\/p>\n<p>So the two entities: house and rooms, are dependent on each other. Room is a part of the House and they two hold a relation of composition.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Composition-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78980\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Composition-in-Java.jpg\" alt=\"Composition in Java\" width=\"585\" height=\"337\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.association;\nimport java.util. * ;\nclass Room {\n  public String roomName;\n  public int roomNo;\n  Room(String name, int number) {\n    this.roomName = name;\n    this.roomNo = number;\n  }\n}\nclass House {\n  private final List &lt; Room &gt; rooms;\n  House(List &lt; Room &gt; rooms) {\n    this.rooms = rooms;\n  }\n  public List &lt; Room &gt; getTotalRoomsInHouse() {\n    return rooms;\n  }\n}\npublic class CompositionDemo {\n  public static void main(String[] args) {\n    Room room1 = new Room(\"Dining Room\", 2);\n    Room room2 = new Room(\"Bed Room\", 5);\n    Room room3 = new Room(\"Living Room\", 3);\n    List &lt; Room &gt; books = new ArrayList &lt; Room &gt; ();\n    books.add(room1);\n    books.add(room2);\n    books.add(room3);\n    House house = new House(books);\n    List &lt; Room &gt; rooms = house.getTotalRoomsInHouse();\n    for (Room room: rooms) {\n      System.out.println(\"The Room Number of \" + room.roomName + \" is: \" + room.roomNo);\n    }\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nThe Room Number of Dining Room is: 2<br \/>\nThe Room Number of Bed Room is: 5<br \/>\nThe Room Number of Living Room is: 3<\/p>\n<h3>Conclusion<\/h3>\n<p>In this article, we discussed Association in Java. If we want to establish connections between two classes then we can use any type of association according to the need.<\/p>\n<p>If the objects are independent of each other, we use Aggregation and if they are dependent then we use Composition.<\/p>\n<p>In Aggregation, if one object dies, the other still remains alive but in Composition, if one dies then the other automatically dies.<\/p>\n<p>We covered everything with real-life so that you can connect it in a better way. We hope you enjoyed reading this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What if there are two classes that have some kind of relationship between them? For example, the classes are Library and Books or Teacher and Student, don\u2019t you think these two classes may be&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78977,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2781,2782,2783,2784,2785,2786,2787],"class_list":["post-78940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-aggregation-in-java","tag-association-in-java","tag-association-in-java-example","tag-composition-in-java","tag-difference-between-aggregation-and-composition-in-java","tag-java-association","tag-types-of-association-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Association - Aggregation and Composition in Java - TechVidvan<\/title>\n<meta name=\"description\" content=\"What is Java Association - Types of Association in java with example - composition &amp; aggregation, difference between aggregation &amp; composition in java,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-association\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Association - Aggregation and Composition in Java - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"What is Java Association - Types of Association in java with example - composition &amp; aggregation, difference between aggregation &amp; composition in java,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-association\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-03T04:30:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Forms-of-Association-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Association - Aggregation and Composition in Java - TechVidvan","description":"What is Java Association - Types of Association in java with example - composition & aggregation, difference between aggregation & composition in java,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-association\/","og_locale":"en_US","og_type":"article","og_title":"Java Association - Aggregation and Composition in Java - TechVidvan","og_description":"What is Java Association - Types of Association in java with example - composition & aggregation, difference between aggregation & composition in java,","og_url":"https:\/\/techvidvan.com\/tutorials\/java-association\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-06-03T04:30:56+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Forms-of-Association-in-Java.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Association &#8211; Aggregation and Composition in Java","datePublished":"2020-06-03T04:30:56+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/"},"wordCount":878,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Forms-of-Association-in-Java.jpg","keywords":["aggregation in java","association in java","association in java example","composition in java","difference between aggregation and composition in java","java association","Types of Association in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-association\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/","url":"https:\/\/techvidvan.com\/tutorials\/java-association\/","name":"Java Association - Aggregation and Composition in Java - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Forms-of-Association-in-Java.jpg","datePublished":"2020-06-03T04:30:56+00:00","description":"What is Java Association - Types of Association in java with example - composition & aggregation, difference between aggregation & composition in java,","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-association\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Forms-of-Association-in-Java.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Forms-of-Association-in-Java.jpg","width":802,"height":420,"caption":"java association"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-association\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Association &#8211; Aggregation and Composition in Java"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78940"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78940\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78977"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}