Preview lessons, content and tests

Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.

2. Searching for something specific? See our text overview of all tests. Scroll right for levels, and lists.

3. Student and Teacher User Guides |  Schemes of Work |   Real Teacher use Videos |


Join 36000+ teachers and students using TTIO.

Inheritance and Object Orientated Programming

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 classParent 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

Single Inheritance

Download PowerPoint


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()

Try it Yourself »


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

Multiple Inheritance

Download PowerPoint

Multi-level Inheritance

Download PowerPoint

Additional Reading and useful links

https://www.w3schools.com/python/python_inheritance.asp

https://www.programiz.com/python-programming/inheritance

https://realpython.com/inheritance-composition-python/

https://docs.python.org/3/tutorial/classes.html

www.teachyourselfpython.com