Start learning free

LESSON 636 OF 673 · OBJECT-ORIENTED PROGRAMMING

Custom Greeting in Python: Overriding Methods

Practice Read the full explanation

You build three classes that greet people.

Greeter is the plain one. PirateGreeter and FormalGreeter inherit from it.

Each class has a method greet(name). Each child overrides it with its own version.

  • In Greeter, greet(name) returns "Hello, <name>".
  • In PirateGreeter, it returns "Ahoy, <name>!".
  • In FormalGreeter, it returns "Good day, <name>.".

Each input line is a kind of greeter, then a person's name. The kinds are greeter, pirate and formal.

For each line, the test code makes that greeter and prints what greet(name) returns.

Where variables have types, the test code keeps each greeter in a Greeter variable. The override still decides the greeting.

C++ and C# mark the parent's method virtual. Kotlin marks it open. C# spells the method Greet.

 

Example 1:

Input:
    greeter Ann
    pirate Bob
    formal Cy
Output:
    Hello, Ann
    Ahoy, Bob!
    Good day, Cy.
Explanation: Each greeter answers with its own version of greet.

Example 2:

Input:
    pirate Mia
    formal Mia
    greeter Mia
Output:
    Ahoy, Mia!
    Good day, Mia.
    Hello, Mia
Explanation: The same name gets three different greetings.

Example 3:

Input:
    formal Lee
    pirate Jack
    formal Max
Output:
    Good day, Lee.
    Ahoy, Jack!
    Good day, Max.
Explanation: No plain greeter here.
Each line still gets its own greeting.

 

Constraints:

  • A name is one word of 1 to 20 English letters.
  • 1 to 100 lines per test case.
STEP 1 OF 6
    Ctrl + Enter to check

    PYTHON · OBJECT-ORIENTED PROGRAMMING

    Custom Greeting in Python, explained

    Problem overview

    Three classes greet people. Greeter is the plain one, and PirateGreeter and FormalGreeter inherit from it. Each class has greet(name), and each child overrides the parent's version with its own:

    Classgreet("Ann")
    GreeterHello, Ann
    PirateGreeterAhoy, Ann!
    FormalGreeterGood day, Ann.

    The tests read lines such as pirate Bob, make that kind of greeter and print what greet("Bob") returns.

    Approach

    To override a method, declare it again in the child class with the same name and the same parameters. When you call greet on an object, Python looks in the object's own class first and only then in its parents, so the child's version wins. The parent's greet is still there for plain Greeter objects.

    Step by step

    1. Declare greet(self, name) in Greeter.
    2. Return "Hello, " followed by name. Mind the comma and the space.
    3. Declare greet again in PirateGreeter, with the same parameters. Same name and same parameters is what makes it an override.
    4. Return "Ahoy, ", then name, then "!".
    5. Declare greet in FormalGreeter the same way.
    6. Return "Good day, ", then name, then ".".

    The solution in Python

    class Greeter:
        def greet(self, name):
            return "Hello, " + name
    
    
    class PirateGreeter(Greeter):
        def greet(self, name):
            return "Ahoy, " + name + "!"
    
    
    class FormalGreeter(Greeter):
        def greet(self, name):
            return "Good day, " + name + "."
    

    Using them for Example 1:

    print(Greeter().greet("Ann"))
    print(PirateGreeter().greet("Bob"))
    print(FormalGreeter().greet("Cy"))
    
    Hello, Ann
    Ahoy, Bob!
    Good day, Cy.
    

    None of the classes has an __init__, so each is created with no arguments. The same name gives three different results depending on which class the object belongs to.

    Python details

    • Python has no keyword for overriding. Writing a method with the same name in the child is all it takes, so a typo in the name creates a new method instead. A child with def great(self, name) leaves greet alone, and calling greet("Bo") still returns Hello, Bo.
    • Python does not check parameters when you override. A child with def greet(self) is accepted, but calling it as greet("Bo") fails with greet() takes 1 positional argument but 2 were given.
    • A child can reuse the parent's version with super(). This subclass returns Hello, Bo!! for "Bo":
    class Excited(Greeter):
        def greet(self, name):
            return super().greet(name) + "!!"
    

    The lesson doesn't need it, since each child builds its whole text itself.

    Common mistakes

    • Different parameters. The override must accept name like the parent does.
    • Misspelled method name. A new method is created and the override never runs.
    • Wrong punctuation. The tests compare whole lines: "Hello, Ann" has no mark at the end, the pirate one ends with ! and the formal one with ..
    • Forgetting the parentheses after the class name. class PirateGreeter: without (Greeter) is a separate class. Its own greet would still run, but it wouldn't be a kind of Greeter, so it wouldn't inherit anything else the parent might gain later.

    Testing

    Example 2 gives the same name to all three kinds. The test lines are pirate Mia, formal Mia and greeter Mia:

    greeters = [PirateGreeter(), FormalGreeter(), Greeter()]
    for g in greeters:
        print(g.greet("Mia"))
    
    Ahoy, Mia!
    Good day, Mia.
    Hello, Mia
    

    The loop doesn't ask which kind each greeter is. It calls g.greet("Mia") on each one, and each object runs the version from its own class. Python variables have no declared types, so no cast or special keyword is needed for this to work. The statement mentions a Greeter variable holding a child; in Python any variable can hold any object, and the override decides the greeting.

    An example with no plain greeter, such as formal Lee, pirate Jack, formal Max, works the same way: each line gets the greeting of its own class.

    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free