Join 36000+ teachers and students using TTIO.
Inheritance is one of the most amazing and useful pillars and properties of Object Orientated Programming. Learn mroe about the three types of inheritance by watching the videos below. Python Inheritance >> Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.
Official Python Documentation on inheritance
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class: Example: Create a class named Person
, with firstname
and lastname
properties, and a printname
method:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
Create a Child Class
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class. Example: Create a class named Student
, which will inherit the properties and methods from the Person
class:
class Student(Person):
pass
https://www.w3schools.com/python/python_inheritance.asp
https://www.programiz.com/python-programming/inheritance
www.teachyourselfpython.com