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.

Class modelling: objects, classes and associations

Class modelling explains how to represent real-world entities as classes and objects, and how associations (relations) between them are shown and constrained in UML class diagrams and models.

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: where an engineer uses this.

Class modelling is used by engineers when they convert requirements into a software structure: to design data structures, APIs and databases. Good class models make implementations easier to write, test and change because they show what objects exist, what state they keep, what operations they provide, and how objects relate to each other.

Key ideas

(Describe concepts from first principles. Bold each term where it is defined.)

  • Object: a concrete entity in a running system with a unique identity, a set of attributes (state) and operations (behaviour). Identity is independent of attribute values: two objects may have identical state but are still different objects if they have different identities.

  • Class: a specification (a blueprint) that defines a set of objects with the same attribute structure and the same operations. Formally: a class C is a pair (S, B) where S is a signature of attributes (names and types) and B is a set of operations with their signatures and semantics. An object o is an instance of class C if o conforms to C's signature and operations.

  • Attribute: a named property of a class that stores part of an object's state; attribute type defines allowed values. Syntax in UML: visibility name : Type [multiplicity] = default {property-string}.

  • Operation (sometimes called method when implemented): a named behaviour belonging to a class, with parameter types and a return type. Operations may be abstract (no implementation) or concrete.

  • State: the collection of current values of an object's attributes. A state's allowed transitions are controlled by the object's operations.

  • Behaviour: the externally observable effect of invoking operations on objects (may include state change, creating other objects, sending messages).

  • Encapsulation: principle that an object's state is accessed only through its operations; enforced by visibility (public +, private -, protected #, package ~ in UML).

  • Association: a structural relationship between instances of two or more classes. Formally an association between class A and class B is a relation R ⊆ Instances(A) × Instances(B) (for binary associations). An association may have a name, role names at each end, multiplicities, and navigability.

  • Multiplicity: a constraint on how many instances at the other end can be associated. Common multiplicity values: 0..1, 1, * (unbounded), 1..*. Multiplicity is a cardinality constraint on the relation.

  • Role: the name (or semantic label) given to an association end, describing the role an instance plays in that relation.

  • Navigability: whether objects of one class can directly reference (navigate to) objects of the other class; shown by an arrowhead on the association line in UML.

  • Association class: when an association itself has attributes, model it as an association class — conceptually a set of tuples (a, b, attr-values).

  • Aggregation and Composition: two special association types that express whole-part relationships. Aggregation (shared) says the part can exist independently of the whole. Composition (strong aggregation) implies ownership and lifecycle dependency: when the whole is destroyed, its parts are destroyed. (Note: exact semantics can be tool-specific — check the UML specification and your tool's documentation.)

Exact, examinable definitions and simple derivations universities commonly expect:

  1. Definition (Object identity): "Two objects are different if they have distinct identities, even if all attributes are equal." Assumption: identity is an abstract token internal to runtime (e.g., pointer). Consequence: equality of state does not imply identity equality.

  2. Definition (Class-instance relation): "An object o is an instance of class C iff o provides all attributes and operations declared by C with matching types." Assumption: a closed-world view of class signature.

  3. Theorem (Association as relation): For a binary association A between classes X and Y with multiplicities m..n at X end and p..q at Y end, the association represents a relation R ⊆ Instances(X) × Instances(Y) such that for any x in Instances(X), p ≤ |{ y | (x,y) ∈ R }| ≤ q, and for any y in Instances(Y), m ≤ |{ x | (x,y) ∈ R }| ≤ n. Derivation (step by step):

    • Start from the binary relation R definition.
    • Interpret multiplicity at Y as a constraint on the image set R(x) = { y | (x,y) ∈ R }.
    • Enforce the bounds for each instance.
    • Thus multiplicities correspond to cardinality bounds on R and its inverse.
  4. Mapping-to-relational-schema rule (commonly asked): For classes mapped to tables, a one-to-many association from A (one) to B (many) is implemented by adding a foreign key column in the 'many' side referencing the 'one' side. Derivation: Because each B must link to exactly one A, store the identity of A in B.

What to draw for class diagrams (exactly):

  • For a class: draw a rectangle with three compartments (top: class name centred and bold; middle: attributes list; bottom: operations list). For each attribute: put visibility symbol then name : Type [multiplicity] = default. For each operation: visibility name(params) : ReturnType.
  • For a binary association: draw a solid line between the two class boxes. At each end put multiplicity (e.g., 1, 0..1, , 1..) near the class, and optionally a role name near the class. For navigability, draw an open arrow at the navigable end.
  • For association class: draw the association line and attach a small class box with a dashed line to the middle of the association line.
  • For aggregation: draw a hollow diamond at the whole end. For composition: draw a filled (black) diamond at the whole end.

Assumptions to state in exam answers: multiplicities are per instance and static unless otherwise noted; association semantics do not imply implementation details; composition lifecycle semantics are a design decision unless the model explicitly states destruction rules.

Formulas

(For a modelling topic, list the syntax to remember.)

attribute syntax: + name : Type [multiplicity] = default (+ is public; - private; # protected; ~ package)

operation syntax: + operationName(param1 : Type, param2 : Type) : ReturnType

multiplicities: 0..1, 1, *, 1..* (read as zero-or-one, exactly one, many, one-or-more)

association as relation: R ⊆ Instances(A) × Instances(B)

cardinality constraint: for x ∈ Instances(A): lower_B ≤ |{ y | (x,y) ∈ R }| ≤ upper_B

Worked examples

(Two or three exam-standard examples with steps. Check outputs.)

Example 1 — Student–Course with Enrollment (association class), and mapping to RDB tables. Problem: Model Students taking Courses; a student may take many courses; a course may have many students. For each enrollment we need the grade and semester.

Step 1: Classes: Student (studentId : String, name : String), Course (courseId : String, title : String). Step 2: Association: Student — Enrollment — Course. Since the association has attributes (grade, semester), we model an association class Enrollment with attributes grade : String, semester : String and multiplicities student * and course * (many-to-many). Draw: Student box, Course box, a line between them with a small attached box labeled Enrollment. Step 3: UML multiplicities: Student end multiplicity * (many), Course end multiplicity * (many). Role names: 'student' at the student end, 'course' at the course end. Step 4: Map to relational schema (standard mapping):

  • Table Student(studentId PK, name)
  • Table Course(courseId PK, title)
  • Table Enrollment(studentId FK → Student.studentId, courseId FK → Course.courseId, grade, semester, PRIMARY KEY(studentId, courseId)) Assumption: a student takes a given course at most once; if repeats across semesters are allowed, include semester in the primary key or add a UNIQUE(studentId, courseId, semester). Final answer (what to write in exam): show diagram as specified and the three table schemas above in a neat list. Bold required: Enrollment becomes a separate table because the association carries attributes.

Example 2 — One-to-many association and foreign key. Problem: Library and BookCopy. A Library can have many BookCopy, each BookCopy belongs to exactly one Library.

Model: Class Library (libraryId : String), Class BookCopy (copyId : String). Association Library 1 — * BookCopy. Navigability: from BookCopy to Library (bookCopy → library) if the system needs to find library quickly.

Mapping to RDB: Table Library(libraryId PK); Table BookCopy(copyId PK, libraryId FK → Library.libraryId). This enforces the multiplicity: each BookCopy row stores one libraryId; the library row need not store copies.

Example 3 — Exam-style long question (model answer): Banking classes and associations. Question: Design classes for Customer, Account and Transaction. Each Customer can have many Accounts. Each Account has many Transactions. A Transaction is linked to exactly one Account and records amount, date, and type.

Model answer (clear steps):

  • Classes and attributes: Customer(customerId : String, name : String), Account(accountNo : String, balance : Money), Transaction(txnId : String, amount : Money, date : Date, type : String).
  • Associations: Customer 1 — * Account (Customer owns Accounts). Account 1 — * Transaction. Composition decision: Account composition Transaction? Usually we model Transaction as a separate entity (no composition) because transactions may be archived independently; justify choice. Multiplicities: Customer end 1, Account end *; Account end 1, Transaction end *.
  • Draw class boxes and association lines with multiplicities. State assumptions: lifecycle rules, visibility, and justification for modelling choices (e.g., why Account balance is attribute and not computed from Transactions).

Common mistakes

  • Thinking association direction makes it functional (an arrow is navigability, not ownership or exclusivity).
  • Confusing aggregation and composition semantics; many students treat hollow diamond same as filled diamond; state lifecycle assumptions explicitly in answers.
  • Omitting visibility and types for attributes and operations in diagrams.
  • Modeling actions (verbs) as classes instead of associations or operations — classes should usually be nouns.

For your exam

(What question forms appear and what full-marks answer needs.)

Question types: definitions (short), draw/interpret class diagrams, convert a class model to relational tables or vice versa, justify modelling choices, design a set of classes and associations for a scenario, explain multiplicity and navigability, show an association class.

What full-marks answers include:

  • Definitions: precise, bold key terms (object, class, association, multiplicity).
  • Diagrams: correct notation (three-compartment class box, multiplicity placed correctly, role names, arrows for navigability, diamonds for aggregation/composition when used) and labels.
  • Reasoning: state assumptions (lifecycle rules, whether association is derived or stored), justify choices like composition vs aggregation vs association.
  • Mapping questions: show explicit schema or code mapping and explain why (e.g., where foreign keys go for one-to-many).

Quick check

Three short questions.

  1. What is the difference between a class and an object?
  2. How do you represent a many-to-many association with attributes in UML?
  3. Where is the foreign key placed when mapping a one-to-many association A (one) — B (many) to a relational table?

Answers: 1) A class is a blueprint (specification); an object is a specific instance with identity and state. 2) Use an association class (or create a separate class representing the association and connect it to both classes). 3) On the 'many' side (in B) — B gets a foreign key referencing A.

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

Stuck on something here?