Which OOP term describes a has-a relationship?
A school timetable program is written using object-oriented programming. Each Timetable object holds an array of Lesson objects. A Lesson object can still exist if the Timetable object is deleted.
Which OOP term describes the relationship between the Timetable class and the Lesson class?
A inheritance
B polymorphism
C containment (aggregation)
D encapsulation
[1]
- 1
Test the relationship in words. A timetable has lessons; a lesson is not a kind of timetable. A "has a" relationship between two classes, where one class holds objects of the other, is containment (aggregation).
- 2
Rule out the others. Inheritance is an "is a" relationship (a subclass derived from a superclass). Polymorphism is one method name behaving differently in different classes. Encapsulation is bundling data with its methods and hiding the data. Answer C (1).
Answer
C
Examiner tip
Examiner tip: say the relationship out loud. "A Timetable has Lessons" → containment; "a Van is a Vehicle" → inheritance. Mark schemes accept "has a" and "is a" as the defining phrases.
Common mistake: choosing A because one class "uses" the other. Inheritance needs one class to be a specialised version of the other, which a lesson is not.