Difficulty: Medium
Correct Answer: It separates the interface of the hierarchy from the concrete implementations, allowing clients to depend on abstract types rather than on specific classes.
Explanation:
Introduction / Context:
In C++, run time polymorphism is usually achieved through base classes with virtual functions and derived classes that override those functions. Introducing a clear interface, often as an abstract base class, is considered good design practice because it decouples users of the hierarchy from specific implementations. This question asks what happens when such an interface is introduced into a polymorphic class hierarchy.
Given Data / Assumptions:
Concept / Approach:
The main effect of introducing a clear interface is to separate what clients expect from how the behaviour is implemented. Clients can hold pointers or references to the base class and call virtual functions without knowing which concrete subclass is in use. This separation improves modularity, allows easier substitution of implementations and simplifies testing, because you can swap in different derived classes as needed. It also encourages design based on abstractions and promotes the open closed principle, where new behaviour can be added via new derived classes without changing existing client code.
Step-by-Step Solution:
Step 1: Focus on the role of an interface or abstract base class in a polymorphic hierarchy.Step 2: Recognise that it defines a contract that all derived classes must follow.Step 3: Note that this contract separates interface from implementation, because the interface specifies what can be done while derived classes specify how it is done.Step 4: Option A states exactly this separation of interface from implementation and the benefit that clients can depend on abstractions.Step 5: Options B, C, D and E describe restrictions or behaviours that conflict with the purpose of polymorphic interfaces, so option A is correct.
Verification / Alternative check:
Consider an abstract base class Shape with a virtual function draw(), and derived classes such as Circle and Rectangle that implement draw(). Client code can work with Shape* or Shape& and call draw() without caring which concrete class is used. If a new class Triangle is added later, existing code does not need to change as long as it uses the Shape interface. This demonstrates how introducing an interface separates usage from implementation.
Why Other Options Are Wrong:
Option B claims that interface and implementation are merged into a monolithic class, which is the opposite of what interfaces are designed to achieve. Option C suggests interfaces remove the need for virtual functions, but interfaces in C++ rely on virtual functions for dynamic dispatch. Option D eliminates inheritance, which would break polymorphism altogether. Option E asserts that all classes become final, which would prevent extension and contradict the goal of flexible hierarchies.
Common Pitfalls:
One mistake is to design interfaces that are too large, forcing derived classes to implement methods they do not need. Another pitfall is to couple client code to concrete classes instead of to the interface, which reduces the benefits of polymorphism. Good practice is to keep interfaces focused and to use them consistently across the hierarchy so that the separation from implementation is clear.
Final Answer:
It separates the interface of the hierarchy from the concrete implementations, allowing clients to depend on abstract types rather than on specific classes.
Discussion & Comments