Method Overriding

noun

say it: “METH-ud oh-ver-RY-ding” /ˈmɛθəd ˌoʊvərˈraɪdɪŋ/

A child class writing its own version of a method it inherited, so its objects use the new version instead of the parent's.

What is method overriding?

Method overriding is when a child class writes its own version of a method it inherited, replacing the parent's version.

class Notification:
    def send(self):
        print("Sending notification")

class TextMessage(Notification):
    def send(self):                 # replaces the parent's send()
        print("Sending text message")

TextMessage().send()  # Sending text message

In a sentence

  • “Through method overriding, each shape class gives its own area() while the parent class only names it.”
  • “TextMessage uses method overriding so its send() texts the customer instead of printing a general notice.”

Level: Journeyman · More Python terms at this level

Updated

In other subjects