CSS 2026Computer SciencePaper I, Q. 4
(a) Why cannot we instantiate an abstract class directly, even though it may contain complete functions? (6) (b) A class library contains many book objects. Should this be modeled using inheritance or composition? Why? (6) (c) Demonstrate method overriding using a base class Account and derived class Current Account. Make balance a protected data member. (8)
#cssnexusBy CSS Nexus Team ·
Original CSS 2026 Computer Science I paper Original CSS 2026 Computer Science II paper Original CSS 2016–2026 Computer Science paper
This answer to the CSS 2026 Computer Science paper (Paper I, Q. 4) was written by the CSS Nexus team. Read the outline first, then see how each point is developed.
Outline
- 1. Introduction
- 2. Part (a): why an abstract class cannot be instantiated
- ✓Incomplete type; pure virtual or abstract members; design intent; compiler enforcement; role of complete functions; constructors still run
- 3. Part (b): library and books, inheritance or composition
- ✓"is-a" vs "has-a"; Liskov substitution; aggregation vs composition; flexibility; where inheritance does fit
- 4. Part (c): method overriding with Account and CurrentAccount
- ✓Program; explanation; output; role of protected and virtual
- 5. Conclusion
Answer
Introduction
The "Gang of Four", Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, condensed a decade of object-oriented experience into one principle: "Favor object composition over class inheritance." (Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994.) All three parts of this question turn on how classes relate to one another: abstract classes define what a family of classes must do, composition and inheritance decide how objects are assembled, and overriding lets a derived class replace inherited behaviour.
Three tools of class design
- Abstract class: a contract
- Composition: has-a
- Inheritance: is-a
- Overriding: specialised behaviour
Part (a): Why an abstract class cannot be instantiated directly
An abstract class is a class that is declared incomplete on purpose. In C++ a class becomes abstract when it has at least one pure virtual function (virtual double area() = 0;); in Java and C# it is marked with the keyword abstract. It cannot be instantiated even when most of its functions are complete, for the following reasons.
1. The type is incomplete.
If even one function has no body, an object of that class would have an operation that cannot be executed. In C++, the virtual table entry for a pure virtual function has no valid implementation, so calling it on such an object would be undefined. The language prevents the object from existing at all. (Reference: Bjarne Stroustrup, The C++ Programming Language, 4th ed., Addison-Wesley, 2013, Section 3.2.2, "Abstract Types".)
2. The language rule forbids it.
The C++ standard states that no objects of an abstract class can be created except as sub-objects of a derived class. Java goes further: a class declared abstract cannot be instantiated even if it has no abstract methods, and attempting new Shape() is a compile-time error. (Reference: ISO/IEC 14882, C++ Standard, [class.abstract]; The Java Language Specification, Section 8.1.1.1.)
3. It represents a concept, not a thing.
"Shape", "Animal" or "Employee" are general ideas; a real object is always a specific circle, dog or manager. The abstract class captures what all of them share, and making it non-instantiable stops programmers from creating meaningless generic objects.
4. The complete functions exist for reuse.
Its concrete functions, such as printDetails() or setName(), are written once and inherited by every derived class. They are complete, but they are meant to be used through a derived object that also supplies the missing parts. This is the basis of the Template Method pattern, in which a complete base-class algorithm calls abstract steps that subclasses fill in. (Reference: Gamma et al., 1994, "Template Method".)
5. It enforces a contract.
Every concrete subclass is forced by the compiler to implement the abstract functions. If it does not, it too remains abstract and cannot be instantiated, so incomplete designs are caught at compile time rather than at run time.
6. Constructors still exist and run.
An abstract class may have constructors and data members. They run when a derived object is created, initialising the base part of that object. The rule is not that the class is unusable; it is that it can be used only as part of a complete derived object or through a pointer or reference to one.
Using an abstract class
- 1
Declare common data and functions
- 2
Mark incomplete operations abstract
- 3
Derive concrete classes
- 4
Implement every abstract function
- 5
Instantiate the derived class
Part (b): A library containing many books, inheritance or composition?
The relationship should be modelled using composition (more precisely, aggregation), not inheritance.
1. "Has-a", not "is-a".
Inheritance models an "is-a" relationship: a CurrentAccount is an Account. A library is not a kind of book; it has many books. The correct design is a Library class holding a collection of Book objects, for example vector<Book> books; in C++ or List<Book> books; in Java. (Reference: Grady Booch et al., Object-Oriented Analysis and Design with Applications, 3rd ed., Addison-Wesley, 2007, Chapter 3.)
2. Inheritance would break substitutability.
Under the Liskov Substitution Principle, an object of a derived class must be usable wherever the base class is expected. If Library inherited from Book, a library could be passed to a function expecting a single book, given an ISBN and an author, which is nonsensical. (Reference: Barbara Liskov, "Data Abstraction and Hierarchy", OOPSLA keynote, 1987.)
3. Inheritance cannot express "many".
A class inherits from Book once; it cannot become "many books". Composition holds any number of book objects and lets books be added and removed at run time with operations such as addBook(), removeBook() and searchByTitle().
4. Aggregation fits the lifetimes.
In UML, composition means the parts die with the whole, while aggregation means the parts can exist independently. Books exist before they join a library and may be transferred to another library, so the relationship is aggregation, shown in UML by a hollow diamond. (Reference: Martin Fowler, UML Distilled, 3rd ed., Addison-Wesley, 2003.)
5. Composition gives looser coupling.
Changes to Book do not ripple through a class hierarchy, and the library can later hold other items (journals, DVDs) through a common interface, exactly as the Gang of Four recommend.
6. Where inheritance does fit.
Inheritance is right for kinds of book: PrintedBook and EBook can both inherit from Book, because each truly is a book. A good design therefore uses both: composition between Library and Book, and inheritance among the types of book.
Library design
- 01Library has many Books: aggregation
- 02Books exist independently
- 03add, remove, search at run time
- 04PrintedBook is-a Book
- 05EBook is-a Book
Part (c): Method overriding with Account and CurrentAccount
Method overriding occurs when a derived class provides its own implementation of a function already defined in its base class, with the same name, parameters and return type. In C++ the base function is declared virtual, so the version that runs is chosen at run time by the actual type of the object (dynamic binding). (Reference: Stroustrup, 2013, Section 20.3.2.)
In the program below, balance is a protected data member: it is hidden from outside code but directly accessible inside the derived class. A current account differs from an ordinary account because it allows an overdraft up to a limit, so it overrides withdraw() and display().
#include <iostream> using namespace std; class Account { protected: double balance; // accessible in derived classes only public: Account(double b) : balance(b) {} virtual ~Account() {} virtual void withdraw(double amount) { if (amount <= balance) balance -= amount; else cout << "Insufficient balance" << endl; } virtual void display() const { cout << "Account balance: " << balance << endl; } }; class CurrentAccount : public Account { double overdraftLimit; public: CurrentAccount(double b, double limit) : Account(b), overdraftLimit(limit) {} void withdraw(double amount) override { // overrides Account::withdraw if (amount <= balance + overdraftLimit) // uses protected balance directly balance -= amount; else cout << "Overdraft limit exceeded" << endl; } void display() const override { // overrides Account::display cout << "Current account balance: " << balance << endl; } }; int main() { Account saving(1000); CurrentAccount current(1000, 500); saving.withdraw(1300); // Insufficient balance saving.display(); // Account balance: 1000 Account& ref = current; // base-class reference to a derived object ref.withdraw(1300); // CurrentAccount::withdraw runs (dynamic binding) ref.display(); // Current account balance: -300 return 0; }
Output
Insufficient balance Account balance: 1000 Current account balance: -300
Explanation.
- The same call withdraw(1300) behaves differently: the ordinary account refuses it, while the current account allows it within the Rs 500 overdraft limit. This is run-time polymorphism.
- Because withdraw() and display() are virtual, the call through the base-class reference ref runs the CurrentAccount versions. Without virtual, the Account versions would run (static binding).
- The keyword override (C++11) asks the compiler to confirm that the function really overrides a base function, catching spelling or signature mistakes.
- balance is protected, so CurrentAccount reads and changes it directly, but code in main() cannot write current.balance = 5000;. This keeps encapsulation while allowing inheritance.
- The virtual destructor ensures that deleting a derived object through a base pointer destroys it correctly.
Overriding at run time
- Base reference ref
- Points to CurrentAccount object
- Virtual table lookup
- CurrentAccount::withdraw executes
Conclusion
An abstract class cannot be instantiated because it is a deliberately incomplete type that defines a contract; its complete functions are reused through concrete subclasses. A library and its books form a "has-a" relationship best modelled by aggregation, with inheritance reserved for genuine kinds of book, which follows the Gang of Four's advice to "favor object composition over class inheritance". Finally, the Account example shows how virtual functions, protected members and overriding together give run-time polymorphism: one interface, different behaviour for each kind of object.
Tags
- #Object-Oriented Programming
- #Abstract Class
- #Composition
- #Inheritance
- #Method Overriding
- #Polymorphism
- #C++
- #CSS Computer Science 2026
- #csssolvedpapers
- #csssolvedpapers2026
- #CSS Computer Science Solved Paper 2026
- #Why cannot we instantiate an abstract class directly
- #A class library contains many book objects
Browse every CSS Computer Science solved past paper or the full library of CSS solved past papers, and compare with the original CSS 2026 Computer Science question paper.
Answered this question differently?
Submit your own answer — approved answers are published with your name.
Get new solved papers on WhatsApp
Follow the CSS Nexus channel to hear first when new answers are published.

Corrections and discussion
Nothing here yet. Spotted a mistake in this answer, or have a suggestion? Tell us below.