Friday , October 20 2017
Home / java / OOPS / What is Aggregation in Java

What is Aggregation in Java

Aggregation:

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

Aggregation in Java Example:

/**
 * @author chandrashekhar
 */
public class AggregationDemo {

    public static void main(String[] args) {
        Address address = new Address();
        address.setDoorNo("201");
        address.setStreet("Gajuwaka");
        address.setCity("Visakhapatnam");
        Employee employee = new Employee();
        employee.setId(112);
        employee.setName("Rahul");
        employee.setAddress(address);
        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;
    }

    @Override
    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;
    }

    @Override
    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 chandrashekhar

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+ ,

Recommended

Java access modifiers

  Java access modifiers determines a particular class can access the data members or methods. …

Leave a Reply

Your email address will not be published. Required fields are marked *