25-Comp-B11 Advanced Software Design · December 2019
Question 22 of 28: Favoring Object Composition Over Class Inheritance
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Notes on this paper
17-Comp-B11 Advanced Software Design — National Exams, December 2019. 3 hours, closed book exam with one aid sheet allowed (written on both sides), no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any five (5) questions in Part I, any three (3) in Part II, any four (4) in Part III, any two (2) in Part IV, and any five (5) in Part V — only the first questions answered, in each part, as they appear in the answer book are marked. All questions carry equal weight, so the 19 questions actually marked (5+3+4+2+5 of 28) each count for 100/19 ≈ 5.26% of the paper. All 28 questions are answered below for completeness.
Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, design principles, testing, dependability; Pressman, Software Engineering: A Practitioner's Approach (9th ed.) — supplementary process and quality coverage; Gamma, Helm, Johnson & Vlissides (GoF), Design Patterns: Elements of Reusable Object-Oriented Software — creational/structural/behavioural pattern catalogue and the "program to an interface, not an implementation" / "favor object composition over class inheritance" principles; Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, visibility, and multiple inheritance semantics; Bertrand Meyer, Object-Oriented Software Construction — design by contract, preconditions/postconditions/class invariants; Barbara Liskov's 1987 substitutability paper for Question 12; Karl Wiegers, Software Requirements (3rd ed.); Myers, The Art of Software Testing, for Question 6.
PART I — General Principles (answer any 5 of 7)
PART V — C++/Java and Modular Programming (answer any 5 of 7)
Question 22: Favoring Object Composition Over Class Inheritance (Part V)
The principle says: when a class needs to reuse another class's behaviour, prefer HOLDING a reference to it (composition) over INHERITING from it, because inheritance exposes the entire base class interface to the subclass (and, transitively, to anyone who obtains a reference typed as the subclass), while composition exposes only what is deliberately forwarded.
Inheritance-based reuse (fragile).
class Stack : public std::vector<int> {
// "reuses" vector's push_back/pop_back as push/pop
};
// Problem: Stack now ALSO exposes vector's entire public interface --
// insert(), erase(), operator[] -- letting client code violate the
// stack discipline (push/pop only) that Stack was meant to enforce.
Composition-based reuse (favored).
class Stack {
private:
std::vector<int> data; // COMPOSED, not inherited
public:
void push(int v) { data.push_back(v); }
int pop() {
int v = data.back();
data.pop_back();
return v;
}
bool isEmpty() const { return data.empty(); }
};
// Only push/pop/isEmpty are reachable -- vector's insert()/erase()/
// operator[] never leak through, so the stack discipline is enforced.
The composed version can also swap its internal representation (a std::deque instead of a std::vector) with zero change to any client, since clients depend only on Stack's own three methods — a flexibility the inheritance-based version does not have, since clients that used the leaked vector interface directly would break.