8.6 Aggregation

Composition is another way to relate two classes. In composition (aggregation), one or more members of a class are objects of one or more other classes. Composition is a ‘‘has-a’’ relation.

In the following program a class Info contains objects of Person and Date class as shown in figure :

aggregation

Program (Person.java)

/**
 * This class stores information about a Person.
 */
public class Person
{
   private String firstname, lastname;

   /**
    * This constructor accepts arguments for the
    * first name, and first name.
    */
   public Person(String firstname, String lastname)
   {
      this.firstname = firstname;
      this.lastname  = lastname;
   }

   /**
    * The set method sets each field.
    */
   public void setName(String firstname, String lastname)
   {
      this.firstname = firstname;
      this.lastname  = lastname;
   }

   /**
    * The toString method returns a string containing
    * the Person information.
    */
   public String toString()
   {
      return firstname + " " + lastname;
   }
}

Program (Date.java)

/**
 * This class stores information about a Date.
 */
public class Date
{
   private int month, day, year;

   /**
    * This constructor accepts arguments for the
    * date, month, and year.
    */
   public Date(int month, int day, int year)
   {
      this.month = month;
      this.day   = day;
      this.year  = year;
   }

   /**
    * The set method sets each field.
    */
   public void setDate(int month, int day, int year)
   {
      this.month = month;
      this.day   = day;
      this.year  = year;
   }

   /**
    * The toString method returns a string containing
    * the date information.
    */
   public String toString()
   {
      return month + "-" + day + "-" + year;
   }
}

Program (Info.java)

/**
 * This class Composing the Date Class with the Person Class.
 */
public class Info
{
   private Person name;
   private Date   dob;

   /**
    * This constructor accepts arguments for the
    * firstname, lastname, date, month, and year.
    */
   public Info(String firstname, String lastname, int month, int day,
               int year)
   {
      name = new Person(firstname, lastname);
      dob  = new Date(month, day, year);
   }

   /**
    * The set method sets each field.
    */
   public void setInfo(String firstname, String lastname, int month,
                       int day, int year)
   {
      name.setName(firstname, lastname);
      dob.setDate(month, day, year);
   }

   /**
    * The toString method returns a string containing
    * the information.
    */
   public String toString()
   {
      String str = "Name : " + name + "\nDate of Birth : " + dob;

      return str;
   }
}

Program (InfoDemo.java)

/**
 * This program demonstrates the Info class.
 */
public class InFoDemo
{
   public static void main(String[] args)
   {
      // Create an Info object.
      Info author = new Info("Alex", "Joseph", 10, 3, 1984);

      // Display the course information.
      System.out.println(author);
   }
}

Output :

Name : Alex Joseph
Date of Birth : 10-3-1984