INHERITANCE
V VIDYASHANKAR
18TH JULY, 2025
INHERITANCE
• If the class one is writing is a specialized version of another class that is
already written, then one can use inheritance
• When one class inherits from another, it takes on the attributes and
methods of the first class
• The original class is called a parent class, and the new class is a child
class which can inherit the methods and attributes ( any or all ) of the
parent
EXAMPLE
THE SUPER() FUNCTION
• It is a special function that allows one to call a method from the
parent class
• In the present context, it tells Python to call the __init__ method from
Car, which gives ElectricCar instance all the attributes defined in that
method
• The name super comes the parent class being a superclass and the
child class being a subclass
ADDING ATTRIBUTES
The attribute of battery size which is set at 75 is will be associated with all
instances of created from the Electric car class but will not be associated
with any instances of the Car class which was created earlier
METHOD OVERRIDING
• Sometimes, we may be interested in a method in a child class with the
same name as a method in the parent class but yet the functionality
would be different
• For example, if there is a method in the parent Car class called
fill_gas_tank but this method is really irrelevant to an Electric car