Python Inheritance
Table Of Contents:
- What Is Inheritance?
- Advantages Of Inheritance?
- Implementing Inheritance.
- Adding The __init__() Method In Child Class.
- Use Of super() Method.
- Adding Properties To Child Class.
- Adding Methods To Child Class.
(1) What Is Inheritance?
- Inheritance enables one class to use all the properties and methods from another class, without writing any extra code in the child class.
- The class from which you are deriving properties and behaviours is called Base Class.
- The class which is using the properties and behaviours of another class is called Child Class.
- This is how you inherit all the properties from your parents.
(2) Advantages Of Inheritance?
- You can have code reusability through inheritance.
- You don’t have to write the same code again and again.
- It also allows for the addition of more features to the class without modifying it.
- It can represent the real-world relationship between objects.
- It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
(3) Implementing Inheritance?
- For implementing inheritance, you need to have a parent class from where you are going to inherit properties.
- In the child class, you need to use the parent class name inside the parenthesis( ), after the child class name.
Example:
Creating Parent Class:
class Car:
def __init__(self,name,color,price):
self.name = name
self.color = color
self.price = price
Creating Child Class:
class BMW(Car):
def car_details(self):
print('Car Name:',self.name)
print('Car Color:',self.color)
print('Car Price:',self.price)
Creating Object Of Child Class:
bmw = BMW('BMW','Black',5000000)
bmw.car_details()
Output:
Car Name: BMW
Car Color: Black
Car Price: 5000000
Note:
- Here you can see that I have inherited the Car class into the BMW class.
- Hence, I can able to access the Car class properties.
(4)Adding The __init__() Method In Child Class.
- When you add the __init__() function in the child class, the child class will no longer inherit the parent’s __init__() function.
- The child’s __init__() function overrides the inheritance of the parent’s __init__() function.
Example
Creating Parent Class:
class Games:
def __init__(self,name,players,famous_player):
self.name = name
self.players = players
self.famous_player = famous_player
def games_details(self):
print('Game Name:',self.name)
print('Game Players:',self.players)
print('Game Famous Player:',self.famous_player)
Creating Child Class:
class Cricket(Games):
def __init__(self,overs,team1,team2):
self.overs = overs
self.team1 = team1
self.team2 = team2
def match_details(self):
print('Total Overs:',self.overs)
print('Team 1:',self.team1)
print('Team 2:',self.team2)
Creating Object Of Child Class:
match = Cricket('Cricket',11,'Sachin')
match.games_details()
Output:
AttributeError Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 match.games_details()
Input In [11], in Games.games_details(self)
6 def games_details(self):
----> 7 print('Game Name:',self.name)
8 print('Game Players:',self.players)
9 print('Game Famous Player:',self.famous_player)
AttributeError: 'Cricket' object has no attribute 'name'
Note:
- As you have declared __init__() method inside the child class, you are not able to access the parent class variables.
(5) Solution To The __init__() Method In Child Class.
- To keep the inheritance of the parent’s __init__() function, add a call to the parent’s __init__() function.
Example
Creating Parent Class:
class Games:
def __init__(self,name,players,famous_player):
self.name = name
self.players = players
self.famous_player = famous_player
def games_details(self):
print('Game Name:',self.name)
print('Game Players:',self.players)
print('Game Famous Player:',self.famous_player)
Creating Child Class:
class Cricket(Games):
def __init__(self,name,players,famous_player):
Games.__init__(self,name,players,famous_player)
Creating Object Of Child Class:
match = Cricket('Cricket',11,'Sachin')
match.games_details()
Output:
Game Name: Cricket
Game Players: 11
Game Famous Player: Sachin
Note:
- Here you can see that we are able to access the parent class variables.
(6) Use Of Super Method.
- super( ) the function makes the child class inherit all the methods and properties from its parent.
- By using the super( ) function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.
Example
Creating Parent Class:
class Games:
def __init__(self,name,players,famous_player):
self.name = name
self.players = players
self.famous_player = famous_player
def games_details(self):
print('Game Name:',self.name)
print('Game Players:',self.players)
print('Game Famous Player:',self.famous_player)
Creating Child Class:
class Cricket(Games):
def __init__(self,name,players,famous_player):
super().__init__(name,players,famous_player) #Removed "self" keyword from here.
Creating Object Of Child Class:
match = Cricket('Cricket',11,'Kohli')
match.games_details()
Output:
Game Name: Cricket
Game Players: 11
Game Famous Player: Kohli
(7) Adding Properties To Child Class
- You can also add a new property to the child class.
- Let us see with an example.
Example
Creating Parent Class:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
Creating Child Class:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
Creating Object Of Child Class:
std = Student("Abhispa", "Pattnaik", 2022)
print(std.graduationyear)
Output:
2022
Note:
- Here you can see that, I have added a new parameter ‘year’ to the __init__( ) method of the child class.
(8) Adding Methods To Child Class
- You can also add a new method to the child class.
- Let us see with an example.
Example
Creating Parent Class:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
Creating Child Class:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "To The Class Of", self.graduationyear)
Creating Object Of Child Class:
std = Student("Abhispa", "Pattnaik", 2022)
std.welcome()
Output:
Welcome Abhispa Pattnaik To The Class Of 2022
Note:
- Here you can see that I have added a new method ‘welcome’ to the child class.