Aggreation & Composition

Aggreation & Composition

Table of contents

Many to 1

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 

        Branch object= new Branch("extc",1234);// first always  create dependent object 
        Student object_Student1= new Student(12,"jeevan",object);
        Student object_Student2= new Student(122,"jeevandsouza",object);
        Student object_Student3= new Student(1223,"sagar",object);




        object_Student1.getter();
        object_Student2.getter();
        object_Student3.getter();

    }
}




 class Student{

    private int student_id;
    private String student_name;

    private Branch branch;
    public Student(int student_id, String student_name, Branch branch){

        this.student_id=student_id;
        this.student_name=student_name;
        this.branch=branch;
    }

    public void getter(){
        System.out.println(student_id);
        System.out.println(branch.getter());
    }


}



class Branch{


    private String branch_name;
    private int branch_number;



    public  Branch(String branch_name, int branch_number){
            this.branch_name=branch_name;
            this.branch_number=branch_number;

    }




    public int getter(){
        return branch_number;
    }
}

Association

  1. We can categorize associations into two types a. Composition: In the case of composition the contained object cannot exist without the
                             container object 
    
    b. Aggregation: In case of aggregation if the contained object would exist even without the
                               container object