As with any typical object-oriented language it is possible in Python to create a
subclass that
inherits properties and methods from a parent or
superclass. This is shown in our attached screenshots. The subclass (or child class) can inherit the methods as is or it can
override them. This means they are redefined in the subclass. When an object is instantiated from the child class the methods not overridden will work just as they do in the parent class. The overridden methods will work as defined in the subclass.
Our little example shows a project manager subclass created based on the parent employee class. It overrides the salary computation to give a bonus rather than deduct an allowance. It also
extends the class by adding a method to return the number of subordinates. Note also the use of the keyword
super in the subclass to invoke a method in the superclass.
|
Click to enlarge view |
No comments:
Post a Comment