πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Association, Composition & Aggregation in Java  


Association (HAS-A relationship)
  • Definition :
    • Association is a relationship where one class uses or interacts with another class by holding a reference to it. It represents a HAS-A relationship in object-oriented programming.
  • Example :
    • Student HAS-A Address
    • Car HAS-A Engine.
    • Laptop HAS-A Processor.
  • How to achieve association ?
    • Association is achieved by declaring object references as instance variables inside a class.
    • We can inject dependent objects using:
      1. Direct Reference Variables
        • Creating the object directly inside the class
      2. Constructor Injection
        • Passing the dependent object through the constructor
      3. Setter Injection
        • Injecting the dependent object using a public setter method
  • Program 1 (Association using Direct Reference Variables) :
    class Address
    {
        String city = "Delhi";
        String country = "India";
    
        void displayAddress()
        {
            System.out.println("City: " + city + ", Country: " + country);
        }
    }
    
    class Student
    {
        String name = "Deepak";
        int rollno = 101;
    
        // Direct reference to another class
        Address address = new Address();  // Object created directly inside the class
    
        void displayInfo()
        {
            System.out.println("Name: " + name + ", Roll No: " + rollno);
            address.displayAddress();
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Student student = new Student();  // No need to pass Address
            student.displayInfo();            // Displays student info along with address
        }
    }
    Output:
    Name: Deepak, Roll No: 101
    City: Delhi, Country: India
  • Program 2 (Association using Constructor Injection) :
    class Engine
    {
        void startEngine()
        {
            System.out.println("Engine starts.");
        }
    }
    
    class Car
    {
        // HAS-A relationship: Car has an Engine
        private Engine engine;
    
        // Constructor Injection: Engine is provided from outside
        Car(Engine engine)
        {
            this.engine = engine;
        }
    
        void startCar()
        {
            engine.startEngine();  // Car uses Engine to start
            System.out.println("Car starts.");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            // create the dependency
            Engine engine = new Engine();
            // inject it into Car
            Car myCar = new Car(engine);
            myCar.startCar();
        }
    }
    Output:
    Engine starts.
    Car starts.
  • Program 3 (Association using Setter Injection) :
    class Processor
    {
        void startProcessor()
        {
            System.out.println("Processor starts processing.");
        }
    }
    
    class Laptop
    {
        // HAS-A relationship: Laptop has a Processor
        private Processor processor;
    
        // Setter Injection: Injecting dependency through setter method
        public void setProcessor(Processor processor)
        {
            this.processor = processor;
        }
    
        void startLaptop()
        {
            processor.startProcessor();
            System.out.println("Laptop starts.");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            // Create the dependency
            Processor processor = new Processor();
    
            // Create the dependent object
            Laptop myLaptop = new Laptop();
    
            // Inject the dependency using setter
            myLaptop.setProcessor(processor);
    
            // Use the dependent object
            myLaptop.startLaptop();
        }
    }
    Output:
    Processor starts processing.
    Laptop starts.

Types of Association:
  1. Aggregation
    • Weak relationship between classes.
    • Objects can exist independently of each other.
    • Example:
      • A Car HAS-A Music Player.
      • The Music Player can be removed, reused, or replaced β€” it can exist without the car.
  2. Composition
    • Strong relationship between classes.
    • One object is fully dependent on the other.
    • Example:
      • A Car HAS-A Engine.
      • The Engine is an essential part of the car β€” if the car is destroyed, the engine has no real standalone meaning.

Associations can be:
  1. One-to-One
  2. One-to-Many
  3. Many-to-One
  4. Many-to-Many
*These are also known as Cardinality of Associations. Cardinality is the count or the number of connections.