What is aggregation ?

What is aggregation ?

Aggregation

Aggregation

  • An aggregation is a special form of association. Access the resources of another .class file without creating any relation, that is the concept of Aggregation in java. It represents a HAS-A relationship.
  • It is a more specialized version of the association relationship. Now, what is an association? In Java, when you think of a class having that has a member of the different type, then there an association can be formed. In a Java class, where there lies an entity reference, it becomes aggression.
  • Imagine the relationship between a doctor and a patient. A doctor can be associated with multiple patients and at the same time, one patient can visit multiple doctors for treatment and/or consultation. Each of these objects has their own life-cycle and there is no owner. In other words, the objects that are part of the association relationship can be created and destroyed independently.

Drawbacks of Aggregation

It is required to create the object of each individual class explicitly.

  • It is also required to remember any numbers corresponding to that object.
  • Java supports 2 keywords to create a relation between two .class files.
  1. Extend
  2. Implements

Example

                Interface A

                {

                }

                Interface B extends A  //interface to interface

                {

                }

                Class C implement B  //class to interface

                {

                }

                Class D extends C  //class to class

                {

                 }

why did aggregation need?

consider the following example. Every student has an address so the relationship between student and address is a Has-A relationship. But if you consider its vice versa then it would not make any sense.

example

class Address
{
   String city;
   String state;
   String country;
   Address(String c, String st, String con)
   {
       this.city =c;
       this.state = st;
       this.country = con;
   }
}
class Student
{
   int roll;
   String Name;
    Address Addr; 
   Student(int roll, String name, Address addr){
       this.roll=roll;
       this.Name=name;
       this.Addr = addr;
   }
   public static void main(String args[]){
       Address ad = new Address( "bbsr", "khurda", "India");
       StudentClass obj = new StudentClass(1234, "satya", bbsr);
       System.out.println(obj.roll);
       System.out.println(obj.Name);
       System.out.println(obj.Addr.city);
       System.out.println(obj.Addr.state);
       System.out.println(obj.Addr.country);
   }
}

Output:

1234

satya

bbsr

khurda

india

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community