LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Predict the Output · Question 11

A parent method using child hooks Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Python program?

main.py
class Report:
    def render(self):
        return f"[{self.header()}] {self.body()}"
    def header(self):
        return "REPORT"
    def body(self):
        return "empty"

class Sales(Report):
    def body(self):
        return "sales up 12%"

class Audit(Report):
    def header(self):
        return "AUDIT"

print(Sales().render(), "|", Audit().render())
  • A [REPORT] empty | [REPORT] empty
  • B [REPORT] sales up 12% | [REPORT] empty
  • C [REPORT] sales up 12% | [AUDIT] empty Correct
  • D AttributeError
Correct Answer: Option C

render() lives in Report, but self.header() and self.body() dispatch to each subclass's overrides.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links