Advanced Algorithms and Recursion: Question 5
Syllabus 20.1
A program defines a class NotificationChannel, with two subclasses, EmailChannel and
SMSChannel, that inherit from it. Each subclass provides its own version of a method called
Send(). A procedure stores a list of NotificationChannel objects. Some are EmailChannel
objects, some are SMSChannel objects, and calls Send() on each object in turn. Each object
automatically executes its own subclass's version of the method, without the procedure needing
to check which subclass any particular object belongs to.
Which object-oriented programming concept does this best illustrate?
Show worked solution Hide worked solution
Worked solution
Matching the scenario to an OOP concept
The syllabus’s OOP terminology includes objects, properties/attributes, methods, classes, inheritance, polymorphism, containment (aggregation) and encapsulation. Each describing a different relationship or behaviour, so it is important to match the scenario to the concept it actually demonstrates rather than one that is simply present in the background.
Here, EmailChannel and SMSChannel do inherit from NotificationChannel (they are
subclasses of it), but that is not the behaviour the question describes. The key detail is that a
single line of code, Send() called on a NotificationChannel object, produces
different results depending on which subclass the object actually is, automatically, with no
type-checking code written by the programmer. This is the definition of polymorphism: the
same method call resolves to different, class-specific behaviour at run time.
In pseudocode, this could be written as:
FOR EACH C IN ChannelList
CALL C.Send()
NEXT C
The same call C.Send() runs EmailChannel’s version of the method (for example, composing and
sending an email) when C refers to an EmailChannel object, and SMSChannel’s version (for
example, formatting and sending a text message) when C refers to an SMSChannel object,
without the loop itself containing any logic to distinguish between them.
Why the other options are wrong
- A (encapsulation): encapsulation is about restricting access to an object’s data, for example by making attributes private and only allowing them to be read or changed through getters and setters. The scenario says nothing about restricting attribute access.
- B (inheritance): inheritance is true of this scenario (
EmailChannelandSMSChanneldo inherit fromNotificationChannel), but inheritance only describes a subclass acquiring members from its superclass. It does not, by itself, explain why the same method call behaves differently for different objects. That additional behaviour is polymorphism. - D (containment/aggregation): containment describes one class holding an instance of
another class as one of its attributes (a “has-a” relationship, e.g. a
Carclass containing anEngineobject). No such relationship is described here.EmailChannelandSMSChannelare subclasses ofNotificationChannel, not objects contained within it.
Final answer
C. This scenario illustrates polymorphism: calling Send() on a NotificationChannel
reference produces different, class-specific behaviour automatically, depending on whether the
underlying object is an EmailChannel or an SMSChannel.