Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Java Array

Java OOPs Misc


Aggregation in Java

If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.

In Aggregation without existing container Object if there is chance of existing contained Object then Container and contain object are weakly assosiated and weak assosiation of nothing but aggregation.

Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.

class Employee{
int emp_id;
String emp_name;
Address address;//Address is a class
...
}


In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.

Why use Aggregation?

  1. For Code Reusability.

example of Aggregation in Java

Suppose department Consist of several professor are contain without existing department there may be a chance of existing professor Object hence department and Professor object are weakly assosiated and the weak assosiation is nothing but aggregation.

package com.ashutosh;
class Operation{
int square(int n){
return n*n;
}
}
class AggregJava64bit{
Operation op;//aggregation
double pi=3.14;
double area(int radius){
op=new Operation();
int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
return pi*rsquare;
}
public static void main(String args[]){
AggregJava64bit c=new AggregJava64bit();
double result=c.area(9);
System.out.println(result);
}
}


Output:254.34

When use Aggregation?

  1. Code reuse is also best achieved by aggregation when there is no is-a relationship.
  2. Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.

Understanding meaningful example of Aggregation

In this example, Employee has an object of Address, address object contains its own informations such as city, state, country etc. In such case relationship is Employee HAS-A address.

package com.ashutosh;
class Address1
{
int streetNum;
String city;
String state;
String country;
Address1(int street, String c, String st, String coun)
{
this.streetNum=street;
this.city =c;
this.state = st;
this.country = coun;
}
}
public class AggregJava64bit

{
int rollNum;
String studentName;
//Creating HAS-A relationship with Address class
Address1 studentAddr;
AggregJava64bit(int roll, String name, Address1 addr){
this.rollNum=roll;
this.studentName=name;
this.studentAddr = addr;
}
public static void main(String args[]){
Address1 ad = new Address1(453, "Noda", "Uttar Pradesh", "India");
AggregJava64bit obj = new AggregJava64bit(102, "Prateek", ad);
System.out.println(obj.rollNum);
System.out.println(obj.studentName);
System.out.println(obj.studentAddr.streetNum);
System.out.println(obj.studentAddr.city);
System.out.println(obj.studentAddr.state);
System.out.println(obj.studentAddr.country);
}
}


Output:102
Prateek
453
Noda
Uttar Pradesh
India

Association in java?

Association establishes relationship between two separate classes through their objects.

Relationship can be

  • One to One
  • One to Many
  • Many to One
  • Many to Many
  • Copyright ©2017 Design& development