Nivaar Exam Prep

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) · Last updated

Notes on this paper

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

PART V — C++/Java and Modular Programming (answer any 5 of 7)

Question 22: Favoring Object Composition Over Class Inheritance (Part V)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

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.