~ OOP,Classes,Python,Understanding the concept of single inheritance in Python


Single Inheritance in Python

*Please Note* - all presentations and code challenges+solutions are available to subscribing members. It's now time to consolidate some of our learning and put it all together to create a library management system. We'll also be looking at the concept of single inheritance and how it is an essential pillar of object orientated programming. Watch the video and then attempt the challenges presented within the code. By the end of it, you should have a working understand of single inheritance, recognising that the principle of inheritance is absolutely crucial to understand when dealing with clases and object orienated programming. You should also be becoming quite familiar with how classes work in practice, and how you can use them in your projects.

Video

Code and Challenge #1

"""
Just like your understanding of genetic inheritance, and the fact child A possibly derives their IQ or sporting
ability from Parent A, we use a similar concept in programming uses classes.
A derived or child class inherits attributes and methods from the base or parent class. The child class
can also have its own methods and attributes. This allows for greater useability. We will look at single
inheritance

Task: Use the code provided: 
1. Add another method in the Course parent class that prints the head office location: Caterham, United Kingdom
2. Add a method in the child class (OOP Course) that prints the ID number of the course: #0023013
3. Create a new object called course2' and print the above methods, demonstrating inheritance

Q: If this is single inheritance, what do you think multiple inheritace is?
"""
class Course:
    name="Python Course"
    contactWebsite="www.teachyourselfpython.com"

    def contactDetails(self):
        print("To contact us, simply go to", self.contactWebsite)

#Course is the base/parent class, and the OOP Class is the child or derived class.
class OOPCourse(Course): #this inherits the attributes and methods of the class Course
#the OOP class can have its own attributes and methods in addition to derived attributes and methods
    def __init__(self):
        self.description="Course on OOP Fundamentals"
        self.trainer="Mrs TeachYourselfPython herself"

    def trainerDetails(self):
        print("This Course is about:",self.description, "and is run by:", self.trainer)



#===================DEMONSTRATION OF SINGLE INHERITANCE =================
course1=OOPCourse() #creating an object for the class OOPCourse
#use the object above to call the methods
course1.contactDetails()
course1.trainerDetails()


Code Example #2

"""
Just like your understanding of genetic inheritance, and the fact child A possibly derives their IQ or sporting
ability from Parent A, we use a similar concept in programming uses classes.
A derived or child class inherits attributes and methods from the base or parent class. The child class
can also have its own methods and attributes. This allows for greater useability. We will look at single
inheritance

Task:
1. Add another method in the Toshiba parent class that prints the head office location: Minato, Tokyo, Japan
2. Add a method in the child class (ToshLaptop) that prints the serial number of the laptop: #0023013
3. Create a new object called 'laptop2' and print the above methods, demonstrating inheritance

Q: If this is single inheritance, what do you think multiple inheritace is?
"""
class Toshiba:
    manufacturer="Toshiba Inc"
    contactWebsite="www.toshiba.com/contact"

    def contactDetails
    (self):
        print("To contact us, simply go to", self.contactWebsite)

#Toshiba is the base/parent class, and Laptop Class is the child or derived class.
class ToshLaptop(Toshiba): #this inherits the attributes and methods of the class Toshiba
#the Laptop class can have its own attributes and methods in addition to derived attributes and methods
    def __init__(self):
        self.yearofManufacture=2017

    def manufactureDetails(self):
        print("This Toshiba laptop was created in the year {} by {}".format(self.yearofManufacture, self.manufacturer))

toshiba_laptop=ToshLaptop() #creating an object for the class ToshLaptop
#use the object above to call the methods

#===================DEMONSTRATION OF SINGLE INHERITANCE =================
toshiba_laptop.manufactureDetails()
toshiba_laptop.contactDetails()


Download Python 3 here: