Java

TOPIC 41 – CLASSES WITH AGGREGATION

 

 

LESSON NOTE

 

 

WHAT IS AGGREGATION?

 

  • Aggregation is the concept of using objects as instance variables for more complex objects.  Note that you have already done this every time that you have used a String as an instance variable.

  • Java fully supports aggregation but not all languages do.

  • There is no new syntax to learn.  It is simply a matter of getting comfortable with the concept.  And in fact, it is possible that you have already worked with classes that use aggregation so this might simply be a little review.

 

EXAMPLE

POINT CLASS

  • Consider the following Point class.

 

public class Point

{

   private double x;

   private double y;

 

         public Point(double xvalue, double yvalue)

         {

            x = xvalue;  

y = yvalue;  

   }

 

               public double getX()

               {

                  return x;

               } 

 

               public double getY()

               {

                  return y;

               } 

}


 

RECTANGLE CLASS WITH AGGREGATION

 

We will now create a Rectangle class that will use two Point objects to represent opposite corners of the rectangle.  This is aggregation.

Here are the specifications:

 

  • Create a Rectangle class following these guidelines:
    • It has two private Point data fields.  One is name bl and represents the bottom left corner of the Rectangle.  The other is named tr and represents the top right corner of the Rectangle.
    • It has a constructor that gets two Point objects as arguments.  The constructor is to give those to the data fields.
    • It has another constructor that gets four double variables named x1, y1, x2 and y2 as arguments.  The constructor creates the points and gives those points to the datafields.
    • It has get methods for both data fields.
    • It has a method named area that calculates and returns the area of the Rectangle.

 

Solution:

 

Here is the solution.  Note that there are comments below.

 

public class Rectangle

{

   private Point bl;  //COMMENT #1

   private Point tr;

 

   public Rectangle (Point p1, Point p2)  //COMMENT #2

   {

      bl = p1;

      tr = p2;

   }

 

   public Rectangle (double x1, double y1, double x2, double y2)

   {

       bl = new Point(x1, y1);  //COMMENT #3

       tr = new Point(x2, y2);

   }

 

   public Point getBL()

   {

      return bl;  //COMMENT #4

   }

 

   public Point getTR()

   {

      return tr;

   }

 

   public double area()

   {

       double width = tr.getX() – bl.getX();  //COMMENT #5

       double height = tr.getY() = bl.getY();

       return width * height;

   }

}

 

COMMENT #1

 

  • Notice that data fields that are objects are declared in the exact same way as ones that are variables.

 

COMMENT #2

 

  • We deal with objects that are constructor arguments in the exact same way as we would for variables. 

 

COMMENT #3

 

  • This is often a difficult part of aggregation.  The constructor gets double values as arguments.  Those double values have to be stored as Points.  Therefore, we have to create the Point objects.

 

COMMENT #4

 

  • The get methods are no different than before.  The data field is a Point and therefore, the return type for the method is the same, a Point.

 

COMMENT #5

 

  • In this section, we find the width and the height of the rectangle by taking the difference in the coordinates of the Points.  Then we simply calculate and return the area.

 

 

REFERENCE VULNERABILITY

 

If you provide a get method (or any other method) for an instance variable that is an object, you may be providing an undesired ability to alter the object.

 

For example, in the Rectangle class above, we can get the Point that represents one of the corners of the rectangle.  If there is a way to change the Point object, then that would effectively change the Rectangle object.  However, in our example, since our Point class is immutable, there is no concern.

 

Similarly, any class that is immutable, will not give be a concern with get methods.  That is why String objects are not a concern to be returned.