Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Advanced class modelling

Advanced class modelling: association classes, multiplicity, navigability, roles, aggregation/composition, abstract classes, interfaces, design by contract, OCL constraints, and mapping to implementation.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: Advanced class modelling is used by engineers to design robust, maintainable object models before coding and to communicate exact structure and constraints to developers and databases. Good class models reduce defects, make testing easier, and clarify responsibilities when translating models to code or schemas.

Key ideas

Explain plainly and define every term before use.

  • Class: A blueprint that defines a set of attributes (data) and operations (methods). Use a rectangle with three compartments: top name, middle attributes, bottom operations.

  • Object: A concrete instance of a class with specific attribute values.

  • Association: A structural relationship between two or more classes indicating links between their objects. Draw as a straight line between class boxes. Label with a role name at each end to explain the object's view.

  • Multiplicity: At an association end, a value or range that states how many instances of the class may be associated. Examples: 1 (exactly one), 0..1 (zero or one), * (zero or more). State formally: "Multiplicity m..n at the B end means: for any object a of class A, the number of associated objects of class B is ≥ m and ≤ n." Draw multiplicity adjacent to the class end on the association line.

  • Navigability: A property of an association end that indicates if objects of one class can refer to objects of the other. Represent with an arrow on the association line pointing toward the navigable class. If both ends navigable, arrowheads both sides or no arrows (bidirectional). In exams, absence of arrowheads is usually interpreted as bidirectional; in UML, it can denote unspecified navigability—state your convention.

  • Role name: A local name for the association end seen from the other class. Use plain words (for example, enrolledCourses at Student end of Student—Course).

  • Association class: When an association itself has attributes or operations, model it as an association class: draw a small class box attached to the association line with a dashed line. Formal mapping: an association class A—B with attributes becomes a new class AC and two associations AC–A and AC–B, with multiplicity 1 at the A and B ends (each AC links to exactly one A and one B) and multiplicity 0..* at the AC ends.

  • Qualified association: An association with a qualifier attribute at one end that narrows multiplicity (acts like an index). Draw a small box on the association end with qualifier name and type.

  • Aggregation: A weak whole-part relation. Denote with an open diamond at the whole end. Semantics: parts may exist independently of the whole. Assume no ownership lifetime implication.

  • Composition: A strong whole-part relation. Denote with a filled (black) diamond at the whole end. Formal definition used in exams: "Composition implies exclusive ownership and lifecycle dependency: if the whole is deleted, the part instances are deleted too; a part instance may belong to at most one whole at any time." State assumption: lifecycle deletion semantics is a modelling decision, not enforced by UML tools.

  • Abstract class: A class that cannot be instantiated; it can define operation signatures or partial behavior. Mark with italics or the {abstract} keyword in the class name compartment. Use to factor common behaviour for subclasses.

  • Generalization (inheritance): A relationship between a more general class (superclass) and more specific classes (subclasses). Draw a solid line from subclass(es) to superclass with a hollow triangle arrowhead at the superclass. You can constrain specialization with {disjoint|overlapping} and {complete|incomplete}.

  • Interface: A collection of operation signatures without attribute storage. In UML: use a lollipop notation or a class box with <<interface>>. Interfaces represent a contract of provided operations.

  • Multiple inheritance vs. interfaces: Multiple inheritance allows a class to inherit implementation from multiple parents. Interfaces provide only signatures and avoid the diamond problem. When modelling for languages without multiple inheritance (Java), prefer interface + single concrete superclass or delegation.

  • Design by contract (DbC): Specify preconditions, postconditions, and invariants for operations and classes. Exact definition: "A precondition is a boolean condition that must be true before an operation executes; a postcondition must be true after successful execution; an invariant must hold for all visible states of an object." Use contracts to refine models and to derive test cases.

  • OCL (Object Constraint Language): A textual language to write precise constraints on UML models. Use OCL for invariants, preconditions (pre:), and postconditions (post:). Example pattern: context ClassName::operation(params) pre: ... post: ....

  • Derived attribute: An attribute whose value is computed from other attributes. Indicate with a leading slash, for example /age.

  • Dependency: A transient relationship showing that a change in one element may affect another. Draw as a dashed arrow.

  • Conversion rules often asked in exams (exact, step-by-step):

    1. Association class to classes: Replace association class with a concrete class AC and two associations AC–A and AC–B. Set multiplicities: at A end 1 and at AC end 0..*; at B end 1 and at AC end 0..*. AC primary key = {A.id, B.id} if mapping to relational schema.
    2. Composition to ownership: If composition whole is W, part P has no independent lifecycle, map P as dependent entity with foreign key to W and cascade delete.
  • Diagrams to draw and label in exams:

    • For an association class example (Student—Course with Enrollment): draw Student box, Course box, a line labeled enrolls connecting them, and a small box Enrollment attached to the line with a dashed line; list attributes of Student (studentId:int), Course (courseId:int), Enrollment (grade:char, date:Date). Add multiplicities: Student end * at Course and Course end * at Student (many-to-many).
    • For composition: draw Car with filled diamond to Wheel; label multiplicity Wheel 4.

Note where modelling choices are assumptions: deletion semantics for composition is a modelling convention and may vary by method.

Formulas

For this programming/modelling topic we list syntax and short rules to remember:

  • ClassName — UML class name (top compartment).
  • attributeName : Type [multiplicity] — attribute syntax in class box.
  • operationName(arg: Type) : ReturnType — operation signature.
  • association ClassA roleA [multA] -------- roleB [multB] ClassB — draw an association line with role names and multiplicities.
  • <<interface>> InterfaceName — interface declaration.
  • /derivedAttr : Type — derived attribute (leading slash).
  • context Class::operation(params) pre: <Boolean expression> post: <Boolean expression> context Class inv: <Boolean expression> — OCL contract template. Use @pre to refer to pre-state values and result to refer to the return value in postconditions.
  • associationClass: attach small class box to association with dashed line — notation rule.

Explain symbols when used: : means type, [] indicates optional multiplicity, << >> denotes stereotypes like <<interface>>.

Worked examples

Example 1 — Association class mapped to relational schema (university-exam standard): Problem: Model students enrolling in courses; each enrollment has a grade (char) and date (Date). Students have studentId:int, name:String. Courses have courseId:int, title:String. Show class diagram and relational tables. Answer steps:

  1. Draw three boxes: Student (studentId:int, name:String), Course (courseId:int, title:String), and Enrollment (grade:char, date:Date) attached to the association line between Student and Course. Put multiplicities * at both ends (many-to-many).
  2. Conversion to relational schema (state formula then numbers not needed):
    • Student(studentId PK, name)
    • Course(courseId PK, title)
    • Enrollment(studentId FK -> Student.studentId, courseId FK -> Course.courseId, grade, date, PRIMARY KEY(studentId, courseId))
  3. Explain why: Association class becomes a separate table holding the attributes of the association; primary key is composite of the two foreign keys.

Example 2 — Composition vs Aggregation modelling answer (long question style): Question: Explain difference and show modelling for Car and Engine (composition) vs Fleet and Car (aggregation). Model answer:

  1. Definitions: Composition means exclusive whole-part with shared lifecycle; Aggregation means weaker whole-part without lifecycle binding.
  2. Draw diagrams: Car box connected to Engine with a filled diamond at Car end pointing to Engine; multiplicity Engine 1 (one engine). Fleet box connected to Car with open diamond at Fleet end; multiplicity Car *.
  3. Explain mapping to implementation: For composition (Car–Engine), represent Engine as a private object created and destroyed by Car (private field, no external ownership). For aggregation (Fleet–Car), Fleet holds references to Car objects; Car lifecyle is independent.
  4. State assumption: lifetime deletion semantics are a design decision; mention how garbage-collected languages differ.

Common mistakes

  • Forgetting to convert an association class into a third table when mapping to relational schema.
  • Treating interfaces as classes with attributes instead of only operation signatures.
  • Omitting preconditions or invariants when asked to write contracts; failing to use context for OCL.
  • Assuming UML enforces semantics (like cascade delete) rather than modelling intent — always state your assumption.

For your exam

Kinds of questions produced:

  • Definitions and short notes (e.g., define association class, composition, OCL).
  • Diagram drawing (draw class diagram with association class, multiplicities, navigability and role names).
  • Derivations/mappings (convert class diagram to relational schema or to implementation decisions).
  • Programs or code sketches (show Java interfaces and classes corresponding to model).
  • Contracts and OCL constraints (write invariants, pre/postconditions).

What a full-marks answer includes:

  • Precise definitions (bold terms are defined) and stated assumptions.
  • Correct UML diagram with labels: class names, attributes with types, association lines, role names, multiplicities, navigability arrows, aggregation/composition diamonds, and association class box if needed.
  • If mapping to implementation, list tables or code with primary keys, foreign keys, or class fields and visibility.
  • If asked for OCL or contracts, include context and correct boolean expressions for pre/post/invariants.
  • Short explanation of modelling choices (e.g., why composition used) and any language-specific implementation note.

Quick check

  1. What notation shows that a class cannot be instantiated?
  2. How do you represent an association that has its own attributes?
  3. What does a filled diamond on an association end mean?

Answers: 1) Use an abstract class (name in italics or {abstract}), 2) Use an association class (small class box attached to the association with a dashed line), 3) A composition (strong whole-part relation with lifecycle dependency).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?