-1

I am new to java and programming overall, today I encountered a problem with these 3 relations. Unfortunately from my materials I can't understand how to implement them. I hope somebody can make this clear for me.

1) Association For example I have 2 classes. Student and School, this is association with multiplicity 1:1, I understand that in each class, I'll have atribute that refers to the other class. But my question is, if In constructor do I declare these reference variable? Do I also create set and get methods for them? What if that association is with multiplicity 1:N ? I have reference variable, that refers to list, do i declare that somehow in constructor too? Do i create set and get methods for that array list?

2) Aggregation From what I understand, in terms of implementation in Java aggregation is nothing but one-way association. So the reference variable is only in one class. Is my hypothesis true?

3) Composition I know that composition is special type of aggregation. So what I think, in terms of implementation in Java, that it'll be one way association too, so the implementation will be same and the only thing that differs is how we recognize this relation outside of programming.

If anybody could help me I would really appreciate it

Sorry that I didn't show any example, true is that It would help me to describe my thoughts.

Lets say we have class, student

public class Student {
    private String name;
    private School school;
   }
   public Student(String namei, School school) {
       this.name = name;
       this.school = school;
    }

If class school, would have an atribute private Student student, it would be an association. But if not, and the only reference atribute would be in class Student, Am I right if I would say that the relation between those classes is Aggregation ?

What if we change it up a little bit and we say, that school, needs to have students. So the only thing we change would be the context, how we think about the subject and nothing else, if we would do that, Would I be right if I would say that the relation would be Composition?

Luke
  • 1
  • 3
  • Better to show us your code attempts of examples of what you think Association, Aggregation and Composition might be like. – Hovercraft Full Of Eels Mar 12 '16 at 14:34
  • Related, although I'm not convinced its a dupe (feel free to cv though): http://stackoverflow.com/questions/885937/difference-between-association-aggregation-and-composition – Jason C Aug 28 '16 at 17:13

1 Answers1

0

School definitely needs a student but it cannot be composition because in composition the container (School in this case) and content (Student in this case) are such that destruction of the one means destruction of the other,,Even when you leave your school for higher studies (Content is gone) the content should still remain,

I would consider the relation between the school and student as aggregation

Aggregation : Here there exists a relationship between the two objects in such a way that one uses other,the contained object (student) is initialized in the main function

Here School is Container and Student is content,it is aggregation because as and when the student is born he is not enrolled to school(it would be so if you would have used Composition),so we need to initialize the student in the main function and pass it as the parameter to the school in the constructor

Aggregation :

class Student
{
    String Name;
int grade;

Student() {}

Student(String Name,int grade)
{

    this.Name=Name;
    this.grade=grade;
}

public String toString()
{

    return(this.Name + " : "+this.grade);
}
}






class School
{
    Student student;
    String SchoolName;

School(Student student,String SchoolName)
{

    this.student=student;
    this.SchoolName=SchoolName;
}

public String toString()
{
    return(this.student + " : "+this.SchoolName);
}

}





class Aggregation
{
public static void main(String[] args)
{
    Student student = new Student("Sourabh",4);

    School school = new School(student,"SBI");

    System.out.println(school);
}
}

If you want to implement it as the composition relation initialize the object student in the constructor of school