ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE
A PaymentMethod type requires every concrete implementation to provide charge() and refund(), but there is no shared implementation at all to reuse between them. Which design fits best?
- A. A single concrete class with both methods implemented directly
- B. A pure interface (or, in C++, a class with only pure virtual functions) declaring charge() and refund()
- C. A set of unrelated static utility methods
- D. An abstract class whose constructor performs heavy shared setup logic
Correct answer: B. A pure interface (or, in C++, a class with only pure virtual functions) declaring charge() and refund()
With no shared implementation to reuse, a pure contract (interface, or a C++ class of only pure virtual functions) is the right tool; an abstract class is for sharing common code, which there is none of here.
More OOP with C++ and Java questions
- Which kind of call in C++ is resolved using static (compile-time) binding rather than dynamic (run-time) binding?
- Which of these can a Java interface still NOT have, even in a modern version such as Java 17?
- A class overrides equals() but does not override hashCode(). What bug does this risk for instances used as keys in a HashMap or stored in…
- Which run-time mechanism lets a call through a base-class pointer invoke the correct derived-class override in C++?
- cpp struct A { void f() { cout << "A::f"; } }; struct B : A {}; struct C : A {}; struct D : B, C {}; int main() { D d; d.f(); } What…
- Why can a private method in a Java superclass never be overridden by a subclass, only unintentionally hidden (i.e. always statically bound)?