Professional Documents
Culture Documents
Association in Java - 2022
Association in Java - 2022
ASSOCIATION
RELATIONSHIPS IN JAVA
1. Inheritance
2. Aggregation
3. Association
4. Composition
RELATIONSHIPS IN JAVA
company itself.
● In this manager don’t have all properties of access card but he uses it to enter ABC premises. In this, the
manager object and the access card object use each other but they have their own object life time. In
other words, they can exist without each other. The most important point in this relationship is that there
is no single owner.
● It represents a relationship between two or more objects where all objects have their own life cycle and
there is no owner. The name of an association specifies the nature of relationship between objects. This
● The third requirement is list of employees (Manager has workers who work under him) denotes the same type of
relationship like association but with a difference that one of them is an owner. So as per the requirement, the
● Employee objects can not belong to any other object. For instance, an Employee object can have the relationship
● But… the Employee object can have its own life time which is completely disconnected from the Manager object
(Manager can have his own life). Looking from a different perspective, it means that if the Manager object is
● The lifetimes of both the objects are the same. In other words, the project will not be successful if
the manager is not good, and the manager will not get good increments if the project has issues.
objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child
TYPES OF ASSOCIATION
●Aggregation
●Composition
INTRODUCTION
WHAT IS AN ASSOCIATION ?
IS-A-RELATIONSHIP
HAS-A-RELATIONSHIP
import java.util.*;
class Mobile {
private String mobile_no;
public String getMobileNo() {
return mobile_no;
}
public void setMobileNo(String mobile_no) {
this.mobile_no = mobile_no;
}
}
ASSOCIATION-CONT.,
class Person {
private String name;
List<Mobile> numbers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
ASSOCIATION-CONT.,
public List<Mobile> getNumbers() {
return numbers;
}
public void setNumbers(List<Mobile> numbers) {
this.numbers = numbers;
}
}
ASSOCIATION-CONT.,
public class AssociationExample {
public static void main(String[] args) {
Person person = new Person();
person.setName("Shubham");
Mobile number1 = new Mobile();
number1.setMobileNo("8868923136");
Mobile number2 = new Mobile();
number2.setMobileNo("8630023310");
ASSOCIATION-CONT.,
List<Mobile> numberList = new ArrayList<Mobile>();
numberList.add(number1);
numberList.add(number2);
person.setNumbers(numberList);
System.out.println(person.getNumbers()
+" are mobile numbers of the person "+
person.getName());
} }
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
https://learn.codemithra.com