Complete the code to ensure the subclass can replace the superclass without errors.
class Bird: def fly(self): print("Flying") class [1](Bird): def fly(self): print("Flying high")
The subclass Eagle correctly extends Bird and overrides fly without breaking behavior, following Liskov Substitution Principle.
Complete the method to follow Liskov Substitution Principle by not throwing unexpected exceptions.
class Rectangle: def set_width(self, width): self.width = width class Square(Rectangle): def set_width(self, width): if width < 0: raise ValueError("Width must be positive") self.width = width def set_height(self, height): if [1]: raise ValueError("Height must be positive") self.width = height
The check height < 0 ensures the method does not accept invalid values, maintaining consistent behavior with the superclass.
Fix the error in the subclass method to respect Liskov Substitution Principle by matching the superclass method signature.
class Vehicle: def start(self, key): print("Vehicle started") class Car(Vehicle): def start(self, [1]): print("Car started")
The subclass method must use the same parameter name key to maintain substitutability and avoid breaking client code.
Fill both blanks to ensure the subclass method signature and behavior follow Liskov Substitution Principle.
class PaymentProcessor: def process_payment(self, amount): pass class CreditCardProcessor(PaymentProcessor): def [1](self, [2]): print(f"Processing credit card payment of {amount}")
The subclass method must have the same name process_payment and parameter amount to maintain substitutability.
Fill all three blanks to correctly implement a subclass that respects Liskov Substitution Principle by maintaining method signature and behavior.
class Logger: def log(self, message): print(message) class FileLogger(Logger): def [1](self, [2]): with open('log.txt', 'a') as f: f.write([3] + '\n')
The subclass method log uses the same parameter message and writes it to a file, preserving the expected behavior and signature.