Lets say that there is a Person. Now each person has some defining properties, like their name, age, sex, height, weight. A person could be a student and in that case, they would have some additional defining properties, for example the school they attend, their id number, their year, their section and their seat number.
Now imagine that you are a programmer trying to describe a student using code... how could you go about writing code that expresses these properties discussed above? Keep in mind that a class Person with the properties name, age, sex, height and weight already exists.
You might think of a few different things that can be done here:
One option is to have a person object as a part of the student object, like so:
# class Person here is unused, basically making the already existing class redundantclassPerson:def__init__(self,name,age,sex,height,weight):self.name=nameself.age=ageself.sex=sexself.height=heightself.weight=weightdefdisplay_information(self):print("Name :",self.name)print("Age :",self.age)print("Sex :",self.sex)print("Height :",self.height)print("Weight :",self.weight)classStudent2:def__init__(self,name,age,sex,height,weight,school,id_no,seat_no,year,section):self.name=nameself.age=ageself.sex=sexself.height=heightself.weight=weightself.school=schoolself.id_no=id_noself.seat_no=seat_noself.year=yearself.section=sectiondefdisplay_information(self):print("Name :",self.name)print("Age :",self.age)print("Sex :",self.sex)print("Height :",self.height)print("Weight :",self.weight)print("School :",self.school)print("ID :",self.id_no)print("Seat :",self.seat_no)print("Year :",self.year)print("Section :",self.section)print()# when there are a lot of function parameters, it is nice to specify which parameters correspond to what# values for better readability and clarity and put them each on their own lineB=Student2(name="Robert",age=14,sex="male",height=160,weight=65,school="SUTD",id_no=1025,seat_no=12,year=1,section="A",)print(B.name+"'s age:",B.age)B.display_information()
The first approach works, but the syntax looks a bit unintuitive, doesn't it?
This is because to create a student object, you have to first make a Person object and then provide that person object to the student constructor, like so A = Student1(Person("John", 15, "male", 170, 70), "SUTD", 1024, 32, 2, "A")
Also, to access a student's name and age, you have to do A.person.name and A.person.age... wouldn't A.name and A.age make more sense?
The 2nd approach fixes this issue but it is also a bit tedious because you have to manually declare all properties of a person inside the student constructor... What if there were not 5, but 100 different properties associated with a person? It would be too unfeasable to manually rewrite them.
This is where inheritance comes into the picture. Inheritance literally allows us to "inherit" the properties of one class (called the super or base class) into another class (called the sub or child class)
# Super/Parent classclassPerson:def__init__(self,name,age,sex,height,weight):self.name=nameself.age=ageself.sex=sexself.height=heightself.weight=weightdefdisplay_information(self):print("Name :",self.name)print("Age :",self.age)print("Sex :",self.sex)print("Height :",self.height)print("Weight :",self.weight)# Base/Sub classclassStudent(Person):def__init__(self,name,age,sex,height,weight,school,id_no,seat_no,year,section):Person.__init__(self,name,age,sex,height,weight)# we can re-use functionality from the super class!self.school=schoolself.id_no=id_noself.seat_no=seat_noself.year=yearself.section=sectiondefdisplay_information(self):Person.display_information(self)# we can re-use functionality from the super class!print("School :",self.school)print("ID :",self.id_no)print("Seat :",self.seat_no)print("Year :",self.year)print("Section :",self.section)# when there are a lot of function parameters, it is nice to specify which parameters correspond to what# values for better readability and clarity and put them each on their own lineA=Student(name="Robin",age=16,sex="male",height=180,weight=75,school="SUTD",id_no=1023,seat_no=3,year=3,section="A",)print(A.name+"'s age:",A.age)A.display_information()
Best practice
The following usages of super class methods in the above example:
Are for educational purposes only, in real python programs, we should make use of the following syntax instead:
1234
# notice that the self parameter has been omittedsuper().__init__(name,age,sex,height,weight)super().display_information()
The reason for doing so is that super() in python does the work of figuring out which super class' function to call and if you end up changing the superclass, you don't have to change all your code everywhere (Also there can be multiple super classes, but that's a story for another day)
Given a class computer, Write a subclass laptop and desktop with the given additional properties:
A computer object has the following properties:
CPU Type
Storage Type
Storage Quantity (in GB)
RAM (in GB)
GPU Type
Write a class for laptop and desktop objects that have the above properties, and the additional properties listed below:
Desktop:
Monitor
Monitor Resolution
Keyboard
Mouse
Laptop:
Monitor Resolution
Is it a touchscreen?
Also write a function that displays all this information
1 2 3 4 5 6 7 8 91011121314151617181920212223
classComputer:def__init__(self,cpu:str,storage_type:str,storage:float,ram:float,gpu:str,):# type hints can also be given to a class' data membersself.cpu:str=cpuself.storage_type:str=storage_typeself.storage:float=storageself.ram:float=ramself.gpu:str=gpudefdisplay_information(self):print("The CPU type is :",self.cpu)print("The Storage type is :",self.storage_type)print("The Stroage is :",self.storage)print("The RAM is :",self.ram)print("The GPU is :",self.gpu)
Given a class computer, Write a subclass laptop and desktop with the given additional properties:
A computer object has the following properties:
CPU Type
Storage Type
Storage Quantity (in GB)
RAM (in GB)
GPU Type
Write a class for laptop and desktop objects that have the above properties, and the additional properties listed below:
Desktop:
Monitor
Monitor Resolution
Keyboard
Mouse
Laptop:
Monitor Resolution
Is it a touchscreen?
Also write a function that displays all this information
1 2 3 4 5 6 7 8 91011121314151617181920212223
classComputer:def__init__(self,cpu:str,storage_type:str,storage:float,ram:float,gpu:str,):# type hints can also be given to a class' data membersself.cpu:str=cpuself.storage_type:str=storage_typeself.storage:float=storageself.ram:float=ramself.gpu:str=gpudefdisplay_information(self):print("The CPU type is :",self.cpu)print("The Storage type is :",self.storage_type)print("The Stroage is :",self.storage)print("The RAM is :",self.ram)print("The GPU is :",self.gpu)
classComputer:def__init__(self,cpu:str,storage_type:str,storage:float,ram:float,gpu:str,):# type hints can also be given to a class' data membersself.cpu:str=cpuself.storage_type:str=storage_typeself.storage:float=storageself.ram:float=ramself.gpu:str=gpudefdisplay_information(self):print("The CPU type is :",self.cpu)print("The Storage type is :",self.storage_type)print("The Stroage is :",self.storage)print("The RAM is :",self.ram)print("The GPU is :",self.gpu)classLaptop(Computer):def__init__(self,cpu:str,storage_type:str,storage:float,ram:float,gpu:str,resolution:str,is_touchscreen:bool,):super().__init__(cpu,storage_type,storage,ram,gpu)self.resolution=resolutionself.is_touchscreen=is_touchscreendefdisplay_information(self):super().display_information()print("The resolution is :",self.resolution)print("Is it a touchscreen :",self.is_touchscreen)classDesktop(Computer):def__init__(self,cpu:str,storage_type:str,storage:float,ram:float,gpu:str,monitor:str,resolution:str,keyboard:str,mouse:str,):super().__init__(cpu,storage_type,storage,ram,gpu)self.monitor=monitorself.resolution=resolutionself.keyboard=keyboardself.mouse=mousedefdisplay_information(self):super().display_information()print("The monitor is :",self.monitor)print("The resolution is :",self.resolution)print("The keyboard is :",self.keyboard)print("The mouse is :",self.mouse)