Date | May 2019 | Marks available | 3 | Reference code | 19M.2.SL.TZ0.11 |
Level | SL | Paper | 2 | Time zone | no time zone |
Command term | Explain | Question number | 11 | Adapted from | N/A |
Question
A generic Event
class is defined as follows:
class Event
{
private String eventID;
private int numberOfRaces;
private Race[] races;
private Race finals;
public Event(String ID, int numberOfRaces)
{
eventID = ID;
races = new Race[numberOfRaces];
for(int i = 0; i < numberOfRaces; i++)
{
races[i] = new Race();
}
finals = new Race();
}
public void addSwimmers()
{
// fills the qualifying heats with swimmers
}
public void fillFinals()
{
// fills the finals race with the best 8 from the qualifying heats
}
// more methods()
}
The class above assumes that the event has more than 8 swimmers and requires qualifying heats. However, an event with less than 9 swimmers has no qualifying heats, so the original class was inherited by a new class .
The same method identifier addSwimmers
is used in both classes Race
and Event
.
Explain why this does not cause a conflict.
Outline two advantages of the OOP feature “inheritance”.
Outline how method overriding can help to create the new class FinalsOnlyEvent
.
Markscheme
Award [3 max].
Each method is defined within its own class.
Each method is called within an object of that class.
Therefore the compiler knows which method to use.
Award [4 max].
Award [1] for an advantage and [1] for an elaboration up to [2 max].
Mark as [2] and [2].
It promotes code reuse;
Because the parent object holds common data and action;
It reduces maintenance overhead;
Because you only have to update the parent;
Award [2 max].
Award [1] for a clear understanding of method overriding and [1] for relating it to this situation up to [2 max].
Method overriding redefines/replaces a method from the inherited class;
The constructor could only instantiate the finals
object;
The method addSwimmers()
could fill finals
directly;
The method fillFinals()
could do nothing;
Examiners report
Many candidates wanted this to be about polymorphism and overriding, but the two occurrences of this methods were quite simply in different classes and would be called and distinguished between by their respective objects.
A variety of reasons were accepted with most candidates going for code-reuse and the extensibility features.
This wasn't well-answered, probably because candidates were unsure of the exact structure of the FinalsOnlyEvent class.