
Class Inheritance
Inheritance allows a class (child class or subclass) to inherit attributes and methods from another class (parent class or superclass). It promotes code reuse and the concept of hierarchy and organization in programming.
Parent class is the class being inherited from.
Child class is the class that inherits from another class.
Create a Parent Class
The syntax for defining a parent class is the same as that for creating any other class, as any class can serve as a parent class.
# Create a classclass Person: # Define class attribute classification = 'mammals'
# Add __init__() function for initializing instances' attributes def __init__(self, name, age, hobby): self.name = name self.age = age self.hobby = hobby
# Add an instance function def introduction(self): print(f"Hi! I'm {self.name} and am {self.age} years old. I love {self.hobby}.")
A class named Person
is created, featuring a class attribute classification
, an __init__()
function for initializing objects' attributes, as well as an instance method called introduction
which prints an introduction of new objects.
Next, create an instance of the Person
class and invoke the introduction
instance method.
# Instantiate an object of the Person classp1 = Person("Alice", 24, 'hiking')
# Invoke the introduction() functionp1.introduction()
Output:
Hi! I'm Alice and am 24 years old. I love hiking.
Create a Child Class
To inherit from a parent class in Python, pass the parent class as a parameter when defining the child class.
Example
Create a child class named Athlete
that inherits properties and methods from the Person
class.
# Create a child class by passing parent class as a parameterclass Athlete(Person): pass
💡 Note: Use the pass
keyword in a class when you don't want to add any properties or methods to it.
Without adding any properties or methods, the Athlete
class inherits all properties and methods from its parent class, Person
.
Now, let's create an instance of the Athlete
class and invoke the instance method introduction
.
athlete1 = Athlete("Mike", "18", "swimming")
athlete1.introduction()
Output:
Hi! I'm Mike and am 18 years old. I love swimming.
The instance athlete1
shares the same properties and methods as the instance p1
because the Athlete
class has inherited the properties and methods from the Person
class.
So far we have created a child class that inherits the properties and methods from its parent. Next, we want the child class to have its own __init__()
function.
class Athlete(Person): def __init__(self, name, age, hobby): pass
However, by adding the __init__()
function to the child class, it no longer inherits the parent's __init__()
function. In our example, this means that the child class loses the ability to assign the name
, age
, and hobby
arguments to instances without explicitly including the code: self.name = name
, self.age = age
, and self.hobby = hobby
.
To maintain the inheritance of the parent's __init__()
function, you can include a call to the parent's __init__()
within the child class.
class Athlete(Person): def __init__(self, name, age, hobby): # To mainain the inheritance of the parent class's __init__() function Person.__init__(self, name, age, hobby)
The super()
Function
Person.__init__(self, name, age, hobby)
only maintains the inheritance of the parent class's __init__()
function
To inherit all properties and functions from a parent class, we can utilize the super()
function. This function allows us to inherit all methods and attributes of the parent class without explicitly mentioning the parent class name.
class Athlete(Person): def __init__(self, name, age, hobby): # This allows the child class to inherit all properties # and methods from the parent class super().__init__(self, name, age, hobby)
Add Properties to the Child Class
Add a property named country
to the Athlete class.
class Athlete(Person): def __init__(self, name, age, hobby, country): # This allows the child class to inherit all properties # and methods from the parent class super().__init__(name, age, hobby) self.country = country
Explanation
def __init__(self, name, age, hobby, country)
: This defines that__init__
function takes four parameters (name, age, hobby, and country).super().__init__(name, age, hobby)
: This line calls the constructor of the parent class (Person) usingsuper()
, passing the name, age, and hobby arguments to the constructor of the parent class.self.country = country
: This line adds a unique attribute to the Athlete class by assigning the country parameter value to the country attribute of the newly created Athlete instance.
Create an instance athlete1
from the Athlete
class and print the instance's country
attribute.
athlete1 = Athlete("Mike", "18", "swimming", "US")print(athlete1.country)
Output:
US
Add Methods to the Child Class
Add a method named report
to the Athlete class.
class Athlete(Person): def __init__(self, name, age, hobby, country): # This allows the child class to inherit all properties # and methods from the parent class super().__init__(name, age, hobby) self.country = country
def report(self): print(f"{self.name}-{self.age}-{self.country}-{self.hobby}")
Create an instance athlete1
of the Athlete
class and call the report
method.
athlete1 = Athlete("Mike", "18", "swimming", "US")athlete1.report()
Output:
Mike-18-US-swimming
The introduction
method can also be called on the instance as it has been inherited through the super()
function.
athlete1.introduction()
Output:
Hi! I'm Mike and am 18 years old. I love swimming.
💡 If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.