---
title: "Create classes from design class diagrams"
subject: "Object Oriented Analysis and Design"
chapter: 4
key: "semester_6/object_oriented_analysis_and_design:classes-from-design-class-diagram"
source: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/classes-from-design-class-diagram
site: BCT Engineering
---

# Create classes from design class diagrams

Object Oriented Analysis and Design · Chapter 4: Implementation

- Asked in: 2069 Poush, 6 marks; 2071 Magh, 8 marks (asked 2 times)
- Video: [design class diagram explained with example](https://www.youtube.com/watch?v=OR0DCV70dcQ) — Karan Jetli Live
- Answer checked by Claude Sonnet 5.5 on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/classes-from-design-class-diagram
- Whole chapter: https://www.bctengineer.com/md/semester_6/object_oriented_analysis_and_design/chapter-4

## Question

Illustrate how can you create classes from design class diagrams and methods from interaction diagrams (Use C#, Java etc.). (With the help of suitable object oriented implementation code, describe the procedure/process of creating class definitions from Design Class Diagrams.)

## Short answer

Design class diagrams map to classes: attributes become fields with visibility, operations become methods, associations become references or collections, and interaction diagrams fill method bodies. For example, Sale.makeLineItem() creates SalesLineItem, and getTotal() delegates to subtotal calculations.

## Answer

A **design class diagram (DCD)** shows software classes with attributes (with types and visibility), method signatures, and associations with navigability and multiplicity. It is converted to code class by class using these rules.

#### Rules of mapping (Java)
1. Each class becomes a class definition. An abstract class uses `abstract`, and an interface becomes `interface`.
2. Each attribute becomes a field with the stated visibility (`-` private, `+` public, `#` protected) and type.
3. Each operation becomes a method with the given signature. The body is filled in using the messages of the **interaction diagram** (sequence or collaboration).
4. Generalization becomes `extends`; realization becomes `implements`.
5. An association with navigability becomes a reference attribute in the source class. Multiplicity 1 gives a field, and 0..* or 1..* gives a collection (`List<T>`).
6. Composition: the whole creates its parts (in the constructor or a factory method).
7. Add constructors, getters and setters.

```text
 +---------------+ 1     1..* +-----------------+
 |     Sale      |<>----------| SalesLineItem   |
 |---------------|            |-----------------|
 | -date: Date   |            | -quantity: int  |
 |---------------|            |-----------------|
 | +getTotal():  |            | +getSubtotal()  |
 |   double      |            |   : double      |
 | +makeLineItem |            +-----------------+
 |  (spec, qty)  |
 +---------------+
```
`SalesLineItem` also has a `*` to 1 association to `ProductSpecification` (with `getPrice()`), which is needed to find the price. It is not drawn above, but it appears in the code.

#### Code for the DCD

```java
import java.util.*;

class ProductSpecification {
    private String itemId;
    private double price;
    public ProductSpecification(String id, double price) { itemId = id; this.price = price; }
    public double getPrice() { return price; }
}

class SalesLineItem {
    private int quantity;
    private ProductSpecification spec;           // association *-1
    public SalesLineItem(ProductSpecification spec, int qty) {
        this.spec = spec; this.quantity = qty;
    }
    public double getSubtotal() { return quantity * spec.getPrice(); }
}

class Sale {
    private Date date = new Date();
    private List<SalesLineItem> lineItems = new ArrayList<>(); // 1..*
    public void makeLineItem(ProductSpecification spec, int qty) {
        lineItems.add(new SalesLineItem(spec, qty));   // Creator
    }
    public double getTotal() {
        double total = 0;
        for (SalesLineItem li : lineItems) total += li.getSubtotal();
        return total;
    }
}
```

#### Creating methods from an interaction diagram
In the sequence diagram, `getTotal()` is sent to `:Sale`, which sends `getSubtotal()` to each `:SalesLineItem`, which sends `getPrice()` to `:ProductSpecification`. This gives exactly the method bodies above: each message sent in the diagram becomes a method call inside the method of the sender. The message `makeLineItem(spec, qty)` to `:Sale`, followed by `create(spec, qty)` to `SalesLineItem`, becomes `makeLineItem()` using `new SalesLineItem(...)`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is an association with multiplicity 1..* mapped to code?
   A. A single int field
   B. A collection such as List<T> in the source class
   C. A static method
   D. A separate package
   Answer: B. Multiplicity 0..* or 1..* becomes a collection; multiplicity 1 becomes a single reference field. (Key point: Association with multiplicity)

2. How are generalization and realization mapped in Java?
   A. extends and implements respectively
   B. implements and extends respectively
   C. Both as extends
   D. Both as fields
   Answer: A. Generalization becomes extends; realization of an interface becomes implements. (Key point: Generalization mapping)

3. How does an interaction diagram give method bodies?
   A. It gives the field types
   B. It gives the package names
   C. Each message sent in the diagram becomes a call inside the sender's method
   D. It gives class names only
   Answer: C. A message from :Sale to :SalesLineItem becomes a call in Sale's method. (Key point: Methods from interaction diagrams)

4. In the code, why does Sale.makeLineItem() use new SalesLineItem(...)?
   A. Sale contains the line items, so it creates them (Creator)
   B. SalesLineItem is abstract
   C. Constructors are static
   D. Sale is a subclass of SalesLineItem
   Answer: A. The whole (composition) creates its parts, following the Creator pattern. (Key point: Creator in code)

## Related in Object Oriented Analysis and Design

- [Development process and mapping design to code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Adevelopment-process-map-design-code) — Object Oriented Analysis and Design, chapter 4
- [Marina class diagram implementation code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram-implementation-code) — Object Oriented Analysis and Design, chapter 4
- [Identify conceptual classes; create domain model](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aconceptual-classes-domain-model-steps) — Object Oriented Analysis and Design, chapter 2
- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1
- [Algorithmic vs object-oriented decomposition](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aalgorithmic-vs-oo-decomposition) — Object Oriented Analysis and Design, chapter 1
- [Visibility symbols +, -, # in classes](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avisibility-signs-class-diagram) — Object Oriented Analysis and Design, chapter 3

## Related in other subjects

- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [General process of creating a PCB](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Apcb-creation-process) — Instrumentation II, chapter 8
- [Intranet implementation procedure and VPN](https://www.bctengineer.com/question.md?key=semester_8%2Finternet_and_intranet%3Aintranet-implementation-procedure-vpn) — Internet and Intranet, chapter 6
