0% found this document useful (0 votes)
4 views13 pages

Lecture 4

The document discusses the concept of association in Java, detailing the four types of relationships between classes: one-to-one, one-to-many, many-to-one, and many-to-many. It explains the differences between IS-A (inheritance) and HAS-A (association) relationships, as well as the two forms of association: aggregation and composition, highlighting their characteristics and examples. The document concludes by comparing association, aggregation, and composition, noting that aggregation is a weaker association compared to the stronger composition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Lecture 4

The document discusses the concept of association in Java, detailing the four types of relationships between classes: one-to-one, one-to-many, many-to-one, and many-to-many. It explains the differences between IS-A (inheritance) and HAS-A (association) relationships, as well as the two forms of association: aggregation and composition, highlighting their characteristics and examples. The document concludes by comparing association, aggregation, and composition, noting that aggregation is a weaker association compared to the stronger composition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 4

- By Shivani Supe
Association
Association, in general terms, refers to the relationship between any two entities. Association in java
is the relationship that can be established between any two classes. These relationships can be of four
types:

1. One-to-One relation
2. One-to-many relation
3. Many-to-one relation
4. Many-to-many relation
● One professor can only be assigned to work in one department. This forms a one-to-one
association between the two classes.
● One professor can be assigned to work in multiple departments. This is a one-to-many
association between the two classes.
● Multiple professors can be assigned to work in one department. This forms a many-to-one
association between the two classes.
● Multiple professors can be assigned to work in multiple departments. This is the many-to-many
association between the two classes.
These associations in java help the objects of one class to communicate with the objects of
the other class.

● So one object will be able to access the data of another object. For example, A
professor entity modeled as an object would be able to access/know the names of all
the departments he works at. And a department object can be able to access/know the
names/details of all the professors that work in it.
● Functionality/Services of one object can be accessible to another object. For
example, A professor who is trying to enroll in a department can be able to verify
whether a department he wants to join has a vacancy. This service(programmatic
method/function) to find whether there’s a departmental vacancy can be provided by
the Department class which the Professor class can access.
//Assign list of department staff //Create two professor objects
Association public void Professor ak = new
setStaff(List<Professor> staff) Professor("Arun Kumar");
import java.util.*; //Department Class { this.staff = staff; } Professor ry = new
//Professor Class class Department { Professor("Rahul Yadav");
//Return the list of staff names
class Professor { private String name; //Create a department
public List<String> getStaff()
private String name; List<Professor> staff; Department cse = new
{ List<String> professor_names Department("CSE");
= new ArrayList<String>();
//Assign professor name //Assign department name
//Add the professors to a list and
for(Professor prof : this.staff) add this list to department
Professor(String name) Department(String name)
{professor_names.add(prof.getN List<Professor> staff_cse = new
{ this.name = name; }
{ this.name = name; ame()); //add name of each ArrayList<Professor>();
//Retrieve name professor to names list }
} staff_cse.add(ak);
public String getName() return professor_names; // return
//Retrieve name names list staff_cse.add(ry);
{
public String getName() }} cse.setStaff(staff_cse);
return this.name;
class Main { System.out.println("The staff of
{
} department " + cse.getName() + "
public static void main(String[] is: " + cse.getStaff());}}
return this.name; }} args) {
Forms of Association in Java
There can be two types of relationships in OOPs:

● IS-A
● HAS-A
1. IS-A (Inheritance)
The IS-A relationship is nothing but Inheritance. The relationships that can be established between
classes using the concept of inheritance are called IS-A relations.

Ex: A parrot is-a Bird. Here Bird is a base class, and Parrot is a derived class, Parrot class inherits all
the properties and attributes & methods (other than those that are private) of base class Bird, thus
establishing inheritance(IS-A) relation between the two classes.

The HAS-A association on the other hand is where the Instance variables of a class refer to objects of
another class. In other words, one class stores the objects of another class as its instance variables
thereby establishing a HAS-A association between the two classes.
Contd…
2. HAS-A (association)
Note: The example program for the association we have discussed in the above section is a HAS-A
association.Because Department class is storing the objects of Professor class as its instance variable
staff ( the Listwhich is storing a list of Professors class objects). And a Department class object can
access these stored Professor objects to store/retrieve information from Professor Class, thereby
creating an association between the two classes.

There are two forms of Association that are possible in Java:

a) Aggregation

b) Composition
Aggregation
Aggregation in java is a form of HAS-A relationship between two classes. It is a relatively more loosely coupled relation
than composition in that, although both classes are associated with each other, one can exist without the other
independently. So Aggregation in java is also called a weak association. Let us look at a simple aggregation example to
understand this better.

Example: Consider the association between a Country class and a Sportsperson class. Here’s how it is defined

● Country class is defined with a name and other attributes like size, population, capital, etc, and a list of all the
Sportspersons that come from it.
● A Sportsperson class is defined with a name and other attributes like age, height, weight, etc.
In a real-world context, we can infer an association between a country and a sports person that hails from that country.
Modeling this relation to OOPs, a Country object has-a list of Sportsperson objects that are related to it. Note that a
sportsperson object can exist with his own attributes and methods, alone without the association with the country object.
Similarly, a country object can exist independently without any association to a sportsperson object. In, other words both
Country and Sportsperson classes are independent although there is an association between them. Hence Aggregation is
also known as a weak association.

Another point to note is that here, the Country object has-a Sportsperson object and not the other way around, meaning
the Country object instance variables store the Sportsperson objects(This will be clear when we look at the java program
in the next section), so the association is one-sided. Thus Aggregation is also known as a unidirectional association.
public List<String> //Sportsperson class
Aggregation getSportPersons()
class SportPerson{
import java.util.*; {
private String name;
//Country class List<SportPerson>
listOfSportPersons = public SportPerson(String
class Country{ this.sportPersons; name) //to assign Sportsperson
name
private String name; List<String> names = new
ArrayList<String>(); {this.name = name;
List<SportPerson> sportPersons;
for(SportPerson sportPerson : }
public Country(String name) //to listOfSportPersons)
assign Country name public String getName()
{ this.name = name; { names.add(sportPerson.getNam { return this.name;
} e()); } }}
public void return names; class Main {
setSportPersons(List<SportPerson> }
sportPersons) //To assign sportspeople public static void main(String[]
public String getName() args) {
{
{return this.name; //Create three Sportsperson
this.sportPersons = sportPersons; objects
}} SportPerson macculum = new
}
SportPerson("Mccullum");
SportPerson dhoni = new SportPerson("Dhoni");
SportPersonk kohli = new SportPerson("Kohli");
//Create a country
Country india = new Country("India");
//Create a arraylist and add the sportspersons
List<SportPerson> listOfSportPersons = new ArrayList<SportPerson>();
sportPersons.add(macculum);
sportPersons.add(dhoni);
sportPersons.add(kohli);
//now add this list to Country Class
india.setSportPersons(listOfSportPersons);
//Outputting the Has-a association between Country and Sportsperson
System.out.println("The sports people from country " + india.getName() + " are " + india.getSportPersons());
}}
Composition
Composition in java is a form of relation that is more tightly coupled. Composition in java is also
called Strong association. This association is also known as Belongs-To association as one class, for
all intents and purpose belongs to another class, and exists because of it. In a Composition association,
the classes cannot exist independent of each other. If the larger class which holds the objects of the
smaller class is removed, it also means logically the smaller class cannot exist. Let us explore this
association clearly with an example

Example: The association between College and Student. Below is how it is defined.

● College class is defined with name and the list of students that are studying in it
● A Student class is defined with name and the college he is studying at.

Here a student must be studying in at least one college if he is to be called Student. If the college class
is removed, Student class cannot exist alone logically, because if a person is not studying in any
college then he is not a student.
Student student2 = new for(Student student : students)
Composition Student("lincoln");
{ names.add(student.getName());
import java.util.*; Student student3 = new
Student("Abraham"); }
//College class
ArrayList<Student> students = return names;
class College{ new ArrayList<Student>(); }}
private String name; students.add(student1); //Student class
ArrayList<Student> studentList; students.add(student2); class Student{
public College(String name) //to assign students.add(student3);
college name private String name;
this.studentList = students;} public Student(String name) //to
{
public String getName() assign Student name
this.name = name;
{return this.name;} { this.name = name;}
}
public List<String> public String getName()
public void setStudentList() //To set getStudentList() //To get students
Students list list { return this.name;
{ //Create three Student objects {List<Student> students = }
this.studentList; }
Student student1 = new
Student("Abe"); List<String> names = new
ArrayList<String>();
class Main {
public static void main(String[] args) {
//Create a College
College college1 = new College("MIB");
//set Student List in College Class
college1.setStudentList();
//Outputting the Has-a association between College and Student
System.out.println("The students studying in " + college1.getName() +
" college are " + college1.getStudentList());
}
}
Difference between association, aggregation, composition in Java
Association in java is one of the types of relations between classes. It has two forms
Aggregation(HAS-A) and Composition(Belongs-to). Aggregation is a relatively weak association,
whereas Composition is a strong association. Composition can be called a more restricted form of
Aggregation. Aggregation can be called the superset of Composition, since all Compositions can are
Aggregations but, not all Aggregations can be called Composition.

You might also like