By using inheritance, we can create classes that inherit all the features of their parent classes and allow us to add new ones. You will discover how to use inheritance in Python in this tutorial.
A key component of object-oriented programming is inheritance.
When a new class is defined, an existing class is hardly changed at all. The new class is referred to as a derived (or child) class, and the base (or parent) class is the one from which it inherits.
Python Inheritance Syntax:
class ParentClass:
Body of parent class
class ChildClass(ParentClass):
Body of derived class
A derived class receives features from the base class from which it can inherit new features. Code becomes more reusable as a result.
Let’s use an example to illustrate how inheritance is used.
class Parent:
def __init__(self, name, familyname):
self.firstname = name
self.lastname = familyname
def show(self):
print(self.firstname, self.lastname)
#Use the Parent class to create an object, and then execute the show method:
x = Parent("Harry", "Styles")
x.show()
class Child(Parent):
print("father of")
x = Child("Willaim", "Shakespear")
x.show()
The child class will no longer inherit the parent’s __init__() function once you add the __init__() function.
Note: The inheritance of the parent’s __init__() function is overridden by the child’s __init__() function.
Add a call to the parent’s __init__() function to maintain inheritance from the parent.
You do not need to specify the parent element’s name when using the super() function because the child element will automatically take its parent’s methods and properties.
class Child(Parent):
def __init__(self, fname, lname):
super().__init__(fname, lname)
You can add properties in child by simply defining variables child _init_
class Child(Parent):
def __init__(self, fname, lname, age):
super().__init__(fname, lname)
self.age = 23
print("My age")
x = Child("Willaim", "Shakespear", 23)
print(x.age)
Private class members of the parent
Because we don’t always want the parent class’s instance variables to be passed down to the child class, we can make some of them private so that the child class won’t have access to them.
The parent class of a child class must be identified. This can be achieved by including the parent class name in the child class definition.
Class Child(Parent):
# parent class
class Parent(object):
# constructor
def __init__(self, surname, nic):
self.surname = surname
self.nic = nic
def show(self):
print(self.surname)
print(self.nic)
# child class
class Details(Parent):
def __init__(self, surname, nic, amount, position):
self.amount = amount
self.position = position
# calling the parent class's __init__
Parent.__init__(self, surname, nic)
# creation of an object variable or an instance
a = Details('Rahul', 886012, 200000, "Intern")
# calling a function of the class Parent using its instance
a.show()
There are various inheritances:
Single inheritance is the process by which a child class derives from just one parent class. Above was an illustration.
Multiple inheritances:
A child class is said to have multiple inheritances when it derives from several parent classes.
Multi-level inheritance:
When we have a parent, child and sub child relationship it’s called multi level inheritance.
Multiple Inheritance Example:
class Parent1(object):
def __init__(self):
self.str1 = "Geek1"
print("Parent1")
class Parent2(object):
def __init__(self):
self.str2 = "Geek2"
print("Parent2")
class Child(Parent1, Parent2):
def __init__(self):
# Calling constructors of Parent1
# and Parent2 classes
Parent1.__init__(self)
Parent2.__init__(self)
print("Child")
def show(self):
print(self.str1, self.str2)
ob = Child()
ob.show()
Multi-level inheritance Example:
When we have a parent, child and sub child relationship it’s called multi level inheritance.
# A Python program to demonstrate inheritance
#Parent or Super class.
# Take note of the object in brackets.
#(In general, an object is made the ancestor of all classes.)
#class “Parent" in Python 3.x is the same as "class Parent(object)."
class Parent(object):
# Constructor
def __init__(self, surname):
self.surname = surname
# To get surname
def getSurame(self):
return self.surname
# Child class of Parent Class
class Child(Parent):
# Constructor
def __init__(self, surname, DOB):
Parent.__init__(self, surname)
self.DOB = DOB
# To get surname
def getDob(self):
return self.DOB
# Sub child Class of Child class
class GrandChild(Child):
# Constructor
def __init__(self, surname, DOB, city):
Child.__init__(self, surname, DOB)
self.city = city
# To get city
def getAddress(self):
return self.city
# Driver code
g = GrandChild("Alee", 1999, "Oslo")
print(g.getSurame(), g.getDob(), g.getAddress())