1. LPU MCA Semester 1
  2. CAB106
  3. Unit 6

CAB106 · Unit 6: Application development using OOP

Object-oriented design principles and UML class diagrams

11 practice questions

Key points

  • A class box has name, attributes and operations
  • + public, - private, # protected
  • Filled diamond is composition; hollow triangle is inheritance
  • Aim for high cohesion and low coupling

A UML class diagram shows classes and their relationships. Each class is a rectangle with three compartments: the name, the attributes and the operations (methods). Visibility is shown with + (public), - (private) and # (protected).

RelationshipUML notation
InheritanceSolid line, hollow triangle at the parent
AggregationHollow diamond at the whole
CompositionFilled diamond at the whole
AssociationPlain line, with multiplicities such as 1 and 0..*
DependencyDashed line with an open arrow

Key design principles: single responsibility (one reason to change), open-closed (open to extension, closed to modification), Liskov substitution (a subclass must be usable wherever its parent is), interface segregation and dependency inversion (depend on abstractions, and pass dependencies in rather than creating them inside). Aim for high cohesion (each class is focused) and low coupling (classes know little about each other). Prefer composition (has-a) to inheritance (is-a) unless the is-a statement is true.

Exam trap: the diamond sits at the whole, and a filled one means composition, a hollow one aggregation.

Try two questions

Question 1 · Tier 3, Apply

A Customer places many Orders, and each Order belongs to exactly one Customer. Which multiplicities fit the association?

  1. 0..* on the Customer side and 1 on the Order side
  2. 1 on both the Customer side and the Order side
  3. 1 on the Customer side and 0..* on the Order side
  4. 0..* on both the Customer side and the Order side
Show the answer

C. One customer relates to zero or more orders, and each order relates to one customer.

Why not A: That reverses the relationship: it says many customers share one order.

Why not B: That would allow only one order per customer.

Why not D: That would allow an order to have many customers.

Question 2 · Tier 3, Apply

Which UML symbol marks a composition relationship?

  1. A hollow diamond at the end of the whole
  2. A hollow triangle at the end of the whole
  3. A filled diamond at the end of the whole
  4. A dashed arrow at the end of the whole
Show the answer

C. A filled diamond means the whole owns its parts, which die with it.

Why not A: A hollow diamond marks aggregation.

Why not B: A hollow triangle marks inheritance.

Why not D: A dashed arrow marks a dependency.