Java – Aggregation

Java Aggregation

An aggregation is a relationship between two classes where one class contains an instance of another class. For example, when an object A contains a reference to another object B or we can say Object A has a HAS-A relationship with Object B, then it is termed as Aggregation in Java Programming.

Use of Java Aggregation

Aggregation in Java helps in reusing the code. Object B can have utility methods and which can be utilized by multiple objects. Whichever class has object B then it can utilize its methods.

Java Aggregation Examples

Example 1

In this example, we’re creating few classes like Vehicle, Speed. A Van class is defined which extends Vehicle class and has a Speed class object. Van class inherits properties from Vehicle class and Speed being its property, we’re passing it from caller object. In output, we’re printing the details of Van object.

packagecom.tutorialspoint;classVehicle{privateString vin;publicStringgetVin(){return vin;}publicvoidsetVin(String vin){this.vin = vin;}}classSpeed{privatedouble max;publicdoublegetMax(){return max;}publicvoidsetMax(double max){this.max = max;}}classVanextendsVehicle{privateSpeed speed;publicSpeedgetSpeed(){return speed;}publicvoidsetSpeed(Speed speed){this.speed = speed;}publicvoidprint(){System.out.println("Vin: "+this.getVin()+", Max Speed: "+ speed.getMax());}}publicclassTester{publicstaticvoidmain(String[] args){Speed speed =newSpeed();
      speed.setMax(120);Van van =newVan();

      van.setVin("abcd1233");
      van.setSpeed(speed);
      van.print();}}

Output

Vin: abcd1233, Max Speed: 120.0

Example 2

In this example, we’re creating few classes like Student, Address. A student can has a address. So we’ve defined an address as an instance variable in Student class. In output, we’re printing the student details.

packagecom.tutorialspoint;classAddress{int strNum;String city;String state;String country;Address(int street,String c,String st,String country){this.strNum = street;this.city = c;this.state = st;this.country = country;}}classStudent{int rno;String stName;Address stAddr;Student(int roll,String name,Address address){this.rno = roll;this.stName = name;this.stAddr = address;}}publicclassTester{publicstaticvoidmain(String[] args){Address ad=newAddress(10,"Bareilly","UP","India");Student st=newStudent(1,"Aashi", ad);System.out.println("Roll no: "+ st.rno);System.out.println("Name: "+ st.stName);System.out.println("Street: "+ st.stAddr.strNum);System.out.println("City: "+ st.stAddr.city);System.out.println("State: "+ st.stAddr.state);System.out.println("Country: "+ st.stAddr.country);}}

Output

Roll no: 1
Name: Aashi
Street: 10
City: Bareilly
State: UP
Country: India

In a unique sense, this is a type of association. Aggregation is a one way directed relationship that precisely expresses HAS-A relationship between classes. Additionally, when two classes are aggregated, terminating one of them has no effect on the other. When compared to composition, it is frequently designated as a weak relationship. In comparison, the parent owns the child entity, which means the child entity cannot be accessed directly and cannot exist without the parent object. Contrarily, in an association, the parent and child entities may both exist in their own right.

HAS-A Relationship

These relationships are mainly based on the usage. This determines whether a certain class HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.

Example

publicclassVehicle{}publicclassSpeed{}publicclassVanextendsVehicle{privateSpeed sp;}

This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple applications.

In Object-Oriented feature, the users do not need to bother about which object is doing the real work. To achieve this, the Van class hides the implementation details from the users of the Van class. So, basically what happens is the users would ask the Van class to do a certain action and the Van class will either do the work by itself or ask another class to perform the action.


Comments

Leave a Reply

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