What is Aggregation in Java

By |2015-04-25T12:40:31+05:30April 7th, 2015|OOPS|


Aggregation in Java is one of the special case of Association. The directional association present in between the objects can also be determined. When a single object ‘has-a’ another object and you will get the aggregation. The directions between the object will be specified with the object containing other object. Aggregation can also be called as “Has-a” relationship.

Example : Employee Has a Address


Aggregation in Java Example:

 * @author chandrashekhar
public class AggregationDemo {

    public static void main(String[] args) {
        Address address = new Address();
        Employee employee = new Employee();
        System.out.println("Employee :: " + employee);


class Employee {

    private int id;
    private String name;
    private Address address;  // Employee Has a Address

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public Address getAddress() {
        return address;

    public void setAddress(Address address) {
        this.address = address;

    public String toString() {
        return "Employee{" + "id=" + id + ", name=" + name + ", address=" + address + '}';


class Address {

    private String doorNo;
    private String street;
    private String city;

    public String getDoorNo() {
        return doorNo;

    public void setDoorNo(String doorNo) {
        this.doorNo = doorNo;

    public String getStreet() {
        return street;

    public void setStreet(String street) {
        this.street = street;

    public String getCity() {
        return city;

    public void setCity(String city) {
        this.city = city;

    public String toString() {
        return "Address{" + "doorNo=" + doorNo + ", street=" + street + ", city=" + city + '}';


Output : Employee{id=112, name=Rahul, address= Address{doorNo=201, street=Gajuwaka, city=Visakhapatnam}}

In the above example program Employee and Address are the two different entities, but the relationship between the Employee and Address is “Has a” relationship. Because Employee has an Address.

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment