Tuesday, April 10, 2007

What is Aggregation and how it maps into a Java class?

An Aggregation is an Association which denotes an "is part of" relationship.

Take a 'Car', for example, it is consisted of an engine,a steering wheel, four tires,seats,gear box,fuel tank,engine oil tank, air filters etc.So all constituents of car are parts of it.

If a car is destroyed/smashed, its parts can still be used separately as spares in other cars,so these parts have individual use even when their conatiner entity is destroyed.

In a Java class, an aggregation can be represented from above example as :

class Car
{
List getTires();
List getSeats();
List getAllParts();
}

2 comments :

Vinay on March 21, 2011 at 4:15 AM said...

Inheritence means --> "is-a" relationship
e.g. HONDA CITY is-a CAR

Aggregation means --> "has-a" relationship
HONDA CITY has-a engine


Hope this makes it clear !!!

Bharat Verma on August 18, 2011 at 11:46 AM said...

Thanks Vinay for clearance ... take care buddy !!

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker