Bird
0
0

Which code correctly implements this?

hard📝 Application Q8 of 15
Python - Polymorphism and Dynamic Behavior
You want to create an abstract base class 'Logger' with an abstract method 'log' that accepts a message string. Then create a subclass 'ConsoleLogger' that prints the message. Which code correctly implements this?
Afrom abc import ABC, abstractmethod class Logger(ABC): @abstractmethod def log(self, message): pass class ConsoleLogger(Logger): def log(self, message): print(message)
Bfrom abc import ABC class Logger(ABC): def log(self, message): pass class ConsoleLogger(Logger): def log(self): print(message)
Cfrom abc import ABC class Logger: @abstractmethod def log(self, message): pass class ConsoleLogger(Logger): def log(self, message): print(message)
Dfrom abc import ABC, abstractmethod class Logger(ABC): @abstractmethod def log(self): pass class ConsoleLogger(Logger): def log(self, message): print(message)
Step-by-Step Solution
Solution:
  1. Step 1: Define abstract base class with correct decorator and method signature

    Logger inherits ABC and uses @abstractmethod on 'log' with 'message' parameter.
  2. Step 2: Implement subclass method matching signature

    ConsoleLogger implements 'log' with 'message' parameter and prints it.
  3. Final Answer:

    Code in option A correctly defines Logger and ConsoleLogger with matching abstract method -> Option A
  4. Quick Check:

    Abstract method and subclass signature match = correct implementation [OK]
Quick Trick: Abstract method and subclass must have matching parameters [OK]
Common Mistakes:
  • Missing @abstractmethod decorator
  • Subclass method missing required parameters
  • Not inheriting ABC in abstract base class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes