ProgrammingHardProduct-company placement · GATE CS
Account.withdraw(amount) works for any amount up to the balance. A subclass FixedDeposit overrides it to throw an exception unless the amount is a multiple of 1000. Code written for Account now fails when given a FixedDeposit. Which principle is broken, and how?
- A. Liskov Substitution, because the subclass strengthened the method's precondition
- B. Open/Closed, because FixedDeposit modified the base class
- C. Interface Segregation, because withdraw is too large an interface
- D. Dependency Inversion, because the client depends on a concrete class
Correct answer: A. Liskov Substitution, because the subclass strengthened the method's precondition
A subtype may weaken preconditions and strengthen postconditions, never the reverse. Requiring multiples of 1000 rejects inputs the base contract accepts, so a FixedDeposit cannot stand in for an Account.
More OOP with C++ and Java questions
- Which call violates the Law of Demeter (talk only to your immediate collaborators) inside an OrderService method that receives an Order…
- How many of these statements about Java methods are correct? 1. A final method can be overloaded in a subclass. 2. A static method can be…
- Classes A, B extends A and C extends B all override m(). Inside C, which way can call A's version of m() on the current object?
- Joshua Bloch recommends implementing a singleton as a single-element enum. Which advantage does this give over a class with a private…
- What is printed? cpp struct P { const char s; P(const char s) : s(s) { cout << s; } ~P() { cout << "~" << s; } }; struct B { P b{"B"}; }…
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { hi(); } void hi()…