Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!You can always override your parent class methods. One reason for overriding parent's methods is that you may want special or different functionality in your subclass.
Example
When the above code is executed, it produces the following output −
Calling child method
To understand inheritance in Python, let us take another example. We use following Employee class as parent class −
Next, we define a SalesOfficer class that uses Employee as parent class. It inherits the instance variables name and salary from the parent. Additionally, the child class has one more instance variable incentive.
We shall use built-in function super() that returns reference of the parent class and call the parent constructor within the child constructor __init__() method.
The getSalary() method is overridden to add the incentive to salary.
Example
Declare the object of parent and child classes and see the effect of overriding. Complete code is below −
When you execute this code, it will produce the following output −
Total salary for Rajesh is Rs 9000 Total salary for Kiran is Rs 11000
Base Overridable Methods
The following table lists some generic functionality of the object class, which is the parent class for all Python classes. You can override these methods in your own class −
Sr.No | Method, Description & Sample Call |
---|---|
1 |
__init__ ( self [,args...] ) Constructor (with any optional arguments) Sample Call : obj = className(args) |
2 |
__del__( self ) Destructor, deletes an object Sample Call : del obj |
3 |
__repr__( self ) Evaluatable string representation Sample Call : repr(obj) |
4 |
__str__( self ) Printable string representation Sample Call : str(obj) |
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.