Content text Day 5 - OOPs Association, Aggregation & Composition.pdf
● ● ● ● ● ● 4. Many-to-Many Association Definition: Multiple objects of Class A can be associated with multiple objects of Class B. Real-life Example: Many Students enroll in many Courses. Many Authors write many Books. This is a bidirectional association. Java Code Example: class Course { String name; List students = new ArrayList<>(); } class Student { String name; List courses = new ArrayList<>(); } Here: Each Student can take multiple Courses Each Course can have multiple Students UML Notation: Student ── * -------- * ──> Course Use-case: Complex relationships with cross-linking. Requires intermediate mapping or join table in DB (many-to-many is not directly supported in most relational DBs). Summary Table Type Cardinality Real-world Example Java Structure One-to-One 1 : 1 Person ↔ Passport Object field One-to-Many 1 : * Teacher ↔ Students List/Set field Many-to-One * : 1 Students ↔ College Single field reference Many-to-Many * : * Students ↔ Courses List in both classes