Easy learning with example program codes

Aggregation in java

Aggregation is a type of HAS-A relationship. Aggregation represents a type of relationship between two objects in which one contain the other’s reference. Two objects can exist independently. If one is deleted other can still exist.

Aggregation in real world:

Let us take the example: A room has a chair. Here room object contain the chair object. Both room and chair exist independently.


 * This is used to show simple aggregation example.
 * @author CodesJava
class Chair{
      public void show(){
            System.out.println("show chair details.");
public class RoomTest {
      public static void main(String args[]){
            //Chair class object in RoomTest class
            Chair obj = new Chair();


show chair details.

Download this example.

Note: Aggregation is mainly used for code re-usability.
Next Topic: Command line arguments in java with example.
Previous Topic: Inheritance in java with examples.

Related Topics:

Method overloading in java with example.
Method overriding in java with example.
Dynamic method dispatch or Runtime polymorphism in java with example.
Association in java with example.
Inheritance in java with examples.
Aggregation in java with example.


Please follow and like us:
Posted in Java   

Core Java Tutorial

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki