Terrill Mckinney
Terrill Mckinney

Reputation: 57

Making a subclass in Python which overrides superclass method

So I'm trying to make a class called Dean and this class has to be able to call upon the super class of say (just printing out a text). I was told to use Professor.say(self, stuff) to call upon the superclass of say but I don't really get that.

My code is as follows:

Class Dean(Professor):

    Professor.say(self, stuff)

    self.say(stuff + "- We need money now, send") 

If it wasn't clear before, the whole point is to make it so that Dean says this for any instance of the superclass say, meaning even if there are other instances of say in subclasses (like the Lecturer or Professor in my other Python question) that it'll still say "- We need money now, send". Answers are appreciated but just tips on where I'm confused are just as good.

Upvotes: 1

Views: 1405

Answers (1)

jscs
jscs

Reputation: 64022

Let me try to rephrase what you have said using the correct terms. You want this subclass of Professor, called Dean, to have a method that overrides a method in its superclass; the method's name is say. The Dean's method should call the superclass's version of the method.

The answer is: you define the overridden method the same way you defined the method in your superclass, using the same name and parameters. Inside that method, you can use the code you were told to use Professor.say(self, stuff) to call the superclass's version of the method before your subclass does whatever it needs to do.

Upvotes: 4

Related Questions