Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a parent class's methods

Tags:

Something that I see people doing all the time is:

class Man(object):
    def say_hi(self):
        print('Hello, World.')

class ExcitingMan(Man):
    def say_hi(self):
        print('Wow!')
        super(ExcitingMan, self).say_hi()  # Calling the parent version once done with custom stuff.

Something that I never see people doing is:

class Man(object):
    def say_hi(self):
        print('Hello, World.')

class ExcitingMan(Man):
    def say_hi(self):
        print('Wow!')
        return super(ExcitingMan, self).say_hi()  # Returning the value of the call, so as to fulfill the parent class's contract.

Is this because I hang with all the wrong programmers, or is it for a good reason?

like image 212
orokusaki Avatar asked Sep 27 '10 06:09

orokusaki


People also ask

Can we override parent class method?

There must be an inheritance connection between classes. All the abstract methods in the parent class should be overridden in the child class. If it declared the methods as static or final, then those methods cannot be overridden.

What does it mean to override a parent class method in a child class?

What Does Overriding Mean? Overriding is an object-oriented programming feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class or one of its parent classes.

Can a superclass override a subclass method?

If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.

Can a child class override parent method Java?

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.


2 Answers

I'd argue that explicitly returning the return value of the super class method is more prudent (except in the rare case where the child wants to suppress it). Especially when you don't know what exactly super is doing. Agreed, in Python you can usually look up the super class method and find out what it does, but still.

Of course the people who wrote the other version might have written the parent class themselves and/or known that it has no return value. In that case they'd have decided to do without and explicit return statement.

like image 61
Manoj Govindan Avatar answered Sep 27 '22 20:09

Manoj Govindan


In the example you give the parent class method has no explicit return statement and so is returning None. So in this one case there's no immediate issue. But note that should someone modify the parent to return a value we now probably need to modify each child.

I think your suggestion is correct.

like image 38
djna Avatar answered Sep 27 '22 21:09

djna