Association in Java
Association is a relation between two separate classes that establishes with the help of their Objects. It specifies the relationship between two or more Objects. Association can be one-to-one, one-to-many, many-to-one, and many-to-many. Let us understand this with real-world examples, suppose the relationship between the bus and the passengers. A bus can have only one driver(one-to-one). Many passengers can associate with the single bus(many-to-one). A single passenger can associate with many different buses(one-to-many). Also, many passengers can associate with the many different buses(many-to-many). One object is associated with another object to use the functionality and services provided by another object.
Association is a relation between two separate classes that establishes with the help of their Objects. It specifies the relationship between two or more Objects. Association can be one-to-one, one-to-many, many-to-one, and many-to-many. Let us understand this with real-world examples, suppose the relationship between the bus and the passengers. A bus can have only one driver(one-to-one). Many passengers can associate with the single bus(many-to-one). A single passenger can associate with many different buses(one-to-many). Also, many passengers can associate with the many different buses(many-to-many). One object is associated with another object to use the functionality and services provided by another object.
// passenger class
class Passenger
{
// passenger name
private String name;
// passenger seat id number
private int seatId;
Passenger(String name, int searId)
{
this.name = name;
this.seatId = searId;
}
public String getPassengerName()
{
return this.namel
}
public int getPassengerId()
{
return this.seatId;
}
}
class Passenger
{
// passenger name
private String name;
// passenger seat id number
private int seatId;
Passenger(String name, int searId)
{
this.name = name;
this.seatId = searId;
}
public String getPassengerName()
{
return this.namel
}
public int getPassengerId()
{
return this.seatId;
}
}
// association between both the
// classes in the main method
class Demo
{
public static void main(String[] args)
{
Bus bus = new bus(""Shree Travels");
Passenger psg = new Passenger(""Sneha", 52);
System.out.println(psg.getPassengerName() + " with seat number " + psg,getPassengerId() + " is a passenger of " + bus.getBusName());
}
}
// classes in the main method
class Demo
{
public static void main(String[] args)
{
Bus bus = new bus(""Shree Travels");
Passenger psg = new Passenger(""Sneha", 52);
System.out.println(psg.getPassengerName() + " with seat number " + psg,getPassengerId() + " is a passenger of " + bus.getBusName());
}
}
Output:
Sneha with seat number 52 is a passenger of Shree Travels
Explanation:
In the above example, two separate classes Bus and Passenger, are associated through their Objects inside the class Demo. In this way, we can establish the relationship between two different classes by using the concept of association. A bus can have many passengers, So it is a one-to-many relationship.
Association is of two types, they are:
1. Aggregation
2. Composition
Let’s discuss the two in detail.
Sneha with seat number 52 is a passenger of Shree Travels
Explanation:
In the above example, two separate classes Bus and Passenger, are associated through their Objects inside the class Demo. In this way, we can establish the relationship between two different classes by using the concept of association. A bus can have many passengers, So it is a one-to-many relationship.
Association is of two types, they are:
1. Aggregation
2. Composition
Let’s discuss the two in detail.
Aggregation
Java Aggregation is a weak association and represents a relationship between an object containing other objects. This represents a part of a whole relationship where a part can exist without a whole. Let’s take an example of the relationship between Group and Person. A Person may belong to multiple Groups. Hence a Group can have multiple Persons. But if we delete a Group, the Person object will not destroy. Aggregation represents the Has-A relationship, unidirectional association, i.e., a one-way relationship. For instance, the group can have persons, but vice versa is not possible and thus unidirectional. In this section, both entries can survive individually, which means ending one entity will not affect the other entity. Hence, both objects are independent in aggregation.
Java Aggregation is a weak association and represents a relationship between an object containing other objects. This represents a part of a whole relationship where a part can exist without a whole. Let’s take an example of the relationship between Group and Person. A Person may belong to multiple Groups. Hence a Group can have multiple Persons. But if we delete a Group, the Person object will not destroy. Aggregation represents the Has-A relationship, unidirectional association, i.e., a one-way relationship. For instance, the group can have persons, but vice versa is not possible and thus unidirectional. In this section, both entries can survive individually, which means ending one entity will not affect the other entity. Hence, both objects are independent in aggregation.
/*Group class contains the list of person
Objects. It is associated which the person
class through its object(s) */
// group class
class Group
{
private String groupName;
private List<Person> persons;
Group(String groupName, List<Person> persons)
{
this.groupName = groupName;
this.persons = persons;
}
}
Objects. It is associated which the person
class through its object(s) */
// group class
class Group
{
private String groupName;
private List<Person> persons;
Group(String groupName, List<Person> persons)
{
this.groupName = groupName;
this.persons = persons;
}
}
// main method
class Demo
{
public static void main(Sting[] args)
{
// creating objects of person class
Person a = new Person("Tanmay", 17);
Person b = new Person("Sam", 18);
Person c = new Person("Tanmay", 19);
Person d= new Person("Khushi", 20);
// making a list of persons belongs to social welfare group
List<Person> p1 = new ArrayList<>();
p1.add(a);
p1.add(c);
// making a list of persons belongs to drama fest group
List<Person> p2 = new ArrayList<>();
p2.add(b);
p2.add(d);
// creating objects of group class
Group swGrp = new Group("Social Welfare", 1);
Group dfGrp = new group("Drama Fest", p2);
// before deleting drama fest group
SYstem.out.println("List of persons in Drama Fest group:");
class Demo
{
public static void main(Sting[] args)
{
// creating objects of person class
Person a = new Person("Tanmay", 17);
Person b = new Person("Sam", 18);
Person c = new Person("Tanmay", 19);
Person d= new Person("Khushi", 20);
// making a list of persons belongs to social welfare group
List<Person> p1 = new ArrayList<>();
p1.add(a);
p1.add(c);
// making a list of persons belongs to drama fest group
List<Person> p2 = new ArrayList<>();
p2.add(b);
p2.add(d);
// creating objects of group class
Group swGrp = new Group("Social Welfare", 1);
Group dfGrp = new group("Drama Fest", p2);
// before deleting drama fest group
SYstem.out.println("List of persons in Drama Fest group:");
for(Person p : p2) {
System.out.println("Person name: " + p.getName() + " , Age: " + p.getAge() + " , Group: Drama Fest");
}
// deleting drama fest group
dfGrp = null;
// after deleting drama fest group
// person list will not destroy
System.out.prnitln("Person name: " + p.getName() + " , Age: " + p.getAge());
}
}
}
System.out.println("Person name: " + p.getName() + " , Age: " + p.getAge() + " , Group: Drama Fest");
}
// deleting drama fest group
dfGrp = null;
// after deleting drama fest group
// person list will not destroy
System.out.prnitln("Person name: " + p.getName() + " , Age: " + p.getAge());
}
}
}
Explanation:
Here, we can see that the two classes Person and Group, are associated with each other with the help of objects. There are two groups social welfare and drama fest. We created these groups by using the person class. The group has a list of persons. We have two people Sam and Khushi, in the Drama Fest group as shown in the output. Afterwards, we deleted this group by setting the instance of group equals to null. But, our list of persons remains undestroyed due to the weak association, i.e., aggregation, even after the group was deleted.
Here, we can see that the two classes Person and Group, are associated with each other with the help of objects. There are two groups social welfare and drama fest. We created these groups by using the person class. The group has a list of persons. We have two people Sam and Khushi, in the Drama Fest group as shown in the output. Afterwards, we deleted this group by setting the instance of group equals to null. But, our list of persons remains undestroyed due to the weak association, i.e., aggregation, even after the group was deleted.
Composition in Java
Java Composition is an association that represents a part of a whole relationship where a part cannot exist without a whole. Let’s take an example of the relationship between School and Room. The school object consists of several rooms. Whenever the school object destroys automatically, all the room objects will be destroyed, i.e., without the existing school object, there is no chance of an existing dependent object. So these are strongly associated, and this relationship is called composition. If a whole is deleted, then all parts are deleted. So composition represents the part-of relationship.
Whenever there is a composition between two entities, the created object cannot exist without the other object. Thus, in composition, both entities are dependent on each other.
Java Composition is an association that represents a part of a whole relationship where a part cannot exist without a whole. Let’s take an example of the relationship between School and Room. The school object consists of several rooms. Whenever the school object destroys automatically, all the room objects will be destroyed, i.e., without the existing school object, there is no chance of an existing dependent object. So these are strongly associated, and this relationship is called composition. If a whole is deleted, then all parts are deleted. So composition represents the part-of relationship.
Whenever there is a composition between two entities, the created object cannot exist without the other object. Thus, in composition, both entities are dependent on each other.
// department class
class Department {
private String name;
// list of activity rooms in a department
private List<ActivityRoom> ar;
Departmnet(List<ActivityRoom> ar)
{
this.ar = ar;
}
// getting total number of colleges
public List<ActivityRoom>getActivityRoomsInDepartment()
{
return ar;
}
}
class Department {
private String name;
// list of activity rooms in a department
private List<ActivityRoom> ar;
Departmnet(List<ActivityRoom> ar)
{
this.ar = ar;
}
// getting total number of colleges
public List<ActivityRoom>getActivityRoomsInDepartment()
{
return ar;
}
}
class Demo {
public static void main(String[] args)
{
// creating the objects of activity room class
ActivityRoom a1= new ActivityRoom("Technical", 601);
ActivityRoom a2 = new AcivityRoom("Business", 602);
ActivityRoom a3 = new ActivityRoom("Economics", 603);
// making the list of activity rooms
List<ActivityRoom> act = new ArrayList<ActivityRoom>();
act.add(a1);
act.add(a2);
act.add(a3);
public static void main(String[] args)
{
// creating the objects of activity room class
ActivityRoom a1= new ActivityRoom("Technical", 601);
ActivityRoom a2 = new AcivityRoom("Business", 602);
ActivityRoom a3 = new ActivityRoom("Economics", 603);
// making the list of activity rooms
List<ActivityRoom> act = new ArrayList<ActivityRoom>();
act.add(a1);
act.add(a2);
act.add(a3);
// Creating the object of department class
Department d = new Department(act);
// making the list of activity rooms in department
List<ActivityRoom> arlist = d.getActivityRoomsInDepartment();
for (ActivityRoom a : arlist) {
System.out.println(a.subject + " activity room with id " + a.id);
}
}
}
Department d = new Department(act);
// making the list of activity rooms in department
List<ActivityRoom> arlist = d.getActivityRoomsInDepartment();
for (ActivityRoom a : arlist) {
System.out.println(a.subject + " activity room with id " + a.id);
}
}
}
Output
Explanation:
Here we have two classes Activity room and Department. A department composed of different subject activity rooms. So, If the department gets destroyed, then All activity rooms within that department will be destroyed, i.e., the activity room can not exist without the department. That’s why it is composition.
Explanation:
Here we have two classes Activity room and Department. A department composed of different subject activity rooms. So, If the department gets destroyed, then All activity rooms within that department will be destroyed, i.e., the activity room can not exist without the department. That’s why it is composition.
Methods in Java
Java method is a block of code or collection of statements grouped together to complete a certain job or operation. This is used to achieve the reusability of code and can be utilized many times. It also gives easy modification and readability of code. A method is executed only when we call or invoke it. We have two categories of methods in java, i.e., pre-defined and user-defined. Predefined methods are the methods that are already defined in the Java class libraries. When a particular method is written by the user or programmer, it is known as a user-defined method. User-defined methods can be modified according to the requirement.
Java method is a block of code or collection of statements grouped together to complete a certain job or operation. This is used to achieve the reusability of code and can be utilized many times. It also gives easy modification and readability of code. A method is executed only when we call or invoke it. We have two categories of methods in java, i.e., pre-defined and user-defined. Predefined methods are the methods that are already defined in the Java class libraries. When a particular method is written by the user or programmer, it is known as a user-defined method. User-defined methods can be modified according to the requirement.