When writing code for a class there are two main things, variables and methods. Variables correspond to the data present in each object. This data is of two types - one the details about the object itself and other the details about this objects relation with other objects.
Details about the object itself are its attributes and details about its relation with other objects are associations. Both get encoded in the class variables.
An example : Student class has its attributes : name, age, roll number, height, weight. Then it is also associated with a Grade - so we need to code a variable named grade which points to a Grade class. Grade class can have its own attributes : number, head count, class teacher, section name. Grade class can have the association back to all students.
Student and Grade class diagram :
The way its code will be written will be something like following :
class Student {
private String name;
private String age;
// ignoring other attributes for simplicity
// this represents association
private Grade grade;
// rest of code will be getters / setters
}
class Grade {
private int number;
private int headCount;
// ignoring other attributes for simplicity
// this represents association
private List<Student> students;
// rest of code will be getters / setters
}
One interesting question is when should an attribute become association or vice versa ? Here it is clear that Student object does not have a Grade as its attribute, but what if one represents grade as just a grade number and say we dont need any more details about the Grade in the system, is grade number then an attribute or an association ? Clearly depending on the system we are implementing and how much data is localized in a class vs how much data is spread across the system, one needs to extract out an attribute to an association. Another example here is : age, height and weight attributes of the Student - what if we had a HealthPersona class, would these still be attributes of student or should one put them in HealthPersona ? Depends what kind of system one is building and if these properties are in wide use across the system then this data should be taken out shared properly across the system and Student will have an association with HealthPersona.