Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

ASSOCIATION IN JAVA

ASSOCIATION
RELATIONSHIPS IN JAVA

1. Inheritance

2. Aggregation

3. Association

4. Composition
RELATIONSHIPS IN JAVA

We will analyse those using an example we can take example of our

company itself.

1. A Manager is an employee of ABC Company.

2. A Manager uses a access card to enter in ABC Company.

3. Manager has employees who work under him.

4. Manager’s salary will get salary on the basis of Project.


INHERITANCE

1. A Manager is an employee of ABC Company.

● IS-A relationship: If we see 1st point we can say manager is an employee so it


uses all the behaviour and other properties of Employee. This is called as
Inheritance. We can see inheritance in below mentioned example means manager
is an employee also it can have its own methods as well.
● Parent class reference can be used to hold child class objects , but by using that
reference we can call only parent class methods but not child specific methods
● We can’t use child class reference to hold parent class objects.
ASSOCIATION IN JAVA

2. A Manager uses a access card to enter in ABC Company.

● 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

is represented by a solid line.


AGGREGATION IN JAVA

3.Manager has employees who work under him.

● 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

Manager object will own Employee objects.

● Employee objects can not belong to any other object. For instance, an Employee object can have the relationship

with swipe but with manager it will be different.

● 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

deleted, the Employee object does not die.


AGGREGATION IN JAVA
COMPOSITION IN JAVA

4. Manager’s salary will get salary on the basis of Project.

● 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.

● It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child

objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child

objects will also be deleted..


ASSOCIATION
COMPOSITION IN JAVA
ASSOCIATION

TYPES OF ASSOCIATION

●Aggregation

●Composition
INTRODUCTION

● Association in Java is a connection or relation between two separate classes that


are set up through their objects
● Association relationship indicates how objects know each other and how they are
using each other’s functionality
● It can be described as a “has-a” relationship between classes. The relationship
between the classes can be bi-directional
● It can be one-to-one, one-to-many, many-to-one and many-to-many.
● It defines the multiplicity between objects
ASSOCIATION
EXAMPLES

● A person can have only one passport. That is a “one-to-one” relationship


●  In a Bank and Employee, a bank can have many employees, So it is a “one-to-
many” relationship
● Similarly, Every city exists in exactly one state, but a state can have many cities,
which is a “many-to-one” relationship
● In between a teacher and a student, multiple students can be associated with a
single teacher and a single student can also be associated with multiple teachers
but both can be created or deleted independently. This is a “many-to-many”
relationship
ASSOCIATION

WHAT IS AN ASSOCIATION ?

Association in Java is a connection or relation between two separate classes that are


set up through their objects. Association relationship indicates how objects know each
other and how they are using each other’s functionality. It can be one-to-one, one-to-
many, many-to-one and many-to-many.
ASSOCIATION
ASSOCIATION

IS-A-RELATIONSHIP

● This refers to inheritance or implementation


● Expressed using keyword “extends”
● Main advantage is code reusability
IMPORTANT POINTS:
● Whatever the parent class has, is by default available to the child. Hence by using
child reference, we can call both parent and child class methods
● Whatever the child class has, by default is not available to parent, hence on the
parent class reference we can only call parent class methods but not child specific
methods
ASSOCIATION

HAS-A-RELATIONSHIP

● Has-A means an instance of one class “has a” reference to an instance of another


class or another instance of same class

● It is also known as “composition” or “aggregation”

● There is no specific keyword to implement HAS-A relationship but mostly we are


depended upon “new” keyword.
ASSOCIATION
ASSOCIATION-PROGRAM

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

[email protected] +91 7815 095 095 +91 9019 921 340

You might also like