0% found this document useful (0 votes)
26 views19 pages

04 Four

Python

Uploaded by

Shahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views19 pages

04 Four

Python

Uploaded by

Shahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Path to

Class
Inheritance

Think of Inheritance, what do you think about?


Well it's probably the same for Python too!
What we already know:

Computers Animals Games

Macbook Windows Cats Dogs Birds RPG Racing

Linux Tiger Bear Single Player

Example: Computers -> Computers is a Class (Family), while MacBook, Windows


& Linux are all Objects. We could create a Class for each type of Computer as
well, taking key data from the parent.
What we currently know:
Here is a car class. There are 4 objects, each with 3 properties.

Car

-Roadrunner -Van -Sport Car -Truck


-Red -Blue -Green -Orange
-4 Wheels -4 Wheels -4 Wheels -4 Wheels
What we currently know:
Here is a car class. There are 4 objects, each with 3 properties.

Single Class for all Objects

Car
Object #4
Object #3
Object #1 Object #2

-Roadrunner -Van -Sport Car -Truck


-Red -Blue -Green -Orange
-4 Wheels -4 Wheels -4 Wheels -4 Wheels
Creating a derived class (child class)
A new Class with the name Vintage. This is Inheriting the car class. This is
an example of Class Inheritance in Python
Everything is inherited from
New class (Child Class) the car class.

Vintage(Car) -All Properties


-All Methods

-Roadrunner -Old Car -VW Bus -Jaguar


-Red -Gray -Orange -Red/Orange
-4 Wheels -4 Wheels -4 Wheels -4 Wheels
-$30,000 -$65,000 -$55,000 -$90,000
Creating a derived class (child class)
This new class also has a new property that is only found here

New class (Child Class) Superclass

Vintage(Car)
Object #1

-Roadrunner -Old Car -VW Bus -Jaguar


-Red -Gray -Orange -Red/Orange
-4 Wheels -4 Wheels -4 Wheels -4 Wheels
-$30,000 -$65,000 -$55,000 -$90,000
Superclasses & Derived Classes:

All Audi's are Cars All sofas are furniture

All dogs are animals All jeans are pants

Route Inheritance Type Note

We do not need a Constructor Method


The Child Class is given only given new
Route One (__init__). We will use the Superclass
methods, no new properties are given
constructor

A new constructor is needed, and we


The Child Class is given both new properties
Route Two must activate the superclass
and new methods
constructor as well
Superclass and Inheritance:

Computers Windows Macbook Linux


Superclass (Parent) Derived Classes (Children)

Games RPG Racing Single Player


Superclass (Parent) Derived Classes (Children)

Example: Computers -> Computers is a Class (Parent), while MacBook, Windows


& Linux are all a sub-class of their own. We can now create more specific objects
using our derived classes (child class).
Inheritance - Route #1

Inheriting everything from a super class


Creating a child class ~ Routes #1:
Creating a Child Class that only needs new methods not properties

When we make an instance of a child class, the superclass


constructor (__init__) will be called and used in the child class

class Main: class Child_Class(Main):


def __init__(self, para1, para2): def method_one(self, parameter1):
self.property1 = para1 x = self.property1 + parameter1
self.property2 = para2 return x
def method_one(self): def method_two(self, parameter1):
#run this code #run this code
Creating a child class ~ Routes #1:
1. When creating a Child class, pass the Superclass in
as a parameter to the Child Class
2. Add any new methods

class Main: class Child_Class(Main):


def __init__(self, para1, para2): def method_one(self, parameter1):
self.property1 = para1 x = self.property1 + parameter1
self.property2 = para2 return x
def method_one(self): def method_two(self):
#run this code #run this code
How inheritance works in our code:
class Main: class UserScore( Main ):
def __init__( self, name, age, location ): def calc_score(self, number ):
self.name = name score = self.age * number
self.age = int( age ) return score
self.location = location
def check_age(self):
def user_info(self): if self.age >= 70:
print("Welcome," , self.name ) return "Senior"
print("You are:", self.age ) elif self.age <= 17:
print("You live in:", self.location ) return "Minor"
else:
*We created a child class called “UserScore”. This will
be a class which uses the superclass properties. We return "Normal"
inherit all the properties from our superclass, which
we can use throughout methods in our child class
How inheritance works in our code:
class Main: class UserScore( Main ):
def __init__( self, name, age, location ): def calc_score(self, number ):
self.name = name score = self.age * number
self.age = int( age ) return score
self.location = location
def check_age(self):
def user_info(self): if self.age >= 70:
print("Welcome," , self.name ) return "Senior"
print("You are:", self.age ) elif self.age <= 17:
print("You live in:", self.location ) return "Minor"
else:
*We created a child class called “UserScore”. This will
be a class which uses the superclass properties. We return "Normal"
inherit all the properties from our superclass, which
we can use throughout methods in our child class
Inheritance - Route #2

Using the super() function to inherit


while creating!
Creating a child class ~ Routes #2:
1. When creating a Child class, pass the
Superclass in as a parameter
2. Add old & new properties
3. Add new methods The constructor in a child
class takes the properties of
class UserScore( Main ): the superclass and any new
def __init__(self, name, age, location, score): properties we create

super().__init__( name, age, location)


self.score = int( score )

super() allows us to inherit all the


def checkAvg(self, list1 ):
properties and methods from the
x = self.score / len(list1) * 100
superclass (parent class)
return x
Creating a child class ~ Routes #2:

1. When creating a Child class, pass the Superclass in


as a parameter
2. Add any new methods The constructor in a child
class takes the properties of
class Child_Class( Main ): the superclass and any new
properties we create
def __init__(self, x , y , z , name , age):
super().__init__( x , y , z ) super() allows us to inherit all the
self.name = name properties and methods from the
superclass (parent class)
self.age = int( age )
def method_two(self, parameter1):
#run this code
Creating a child class ~ Routes #2:

1. When creating a Child class, pass the Superclass in


as a parameter
2. Add any new methods The constructor in a child
class takes the properties of
class Child_Class( Main ): the superclass and any new
properties we create
def __init__(self, x , y , z , name , age):
super().__init__( x , y , z ) super() allows us to inherit all the
self.name = name properties and methods from the
self.age = int( age ) superclass (parent class)

def method_two(self, parameter1): When we create an object of the child


#run this code class, the superclass constructor (__init__)
is automatically called and used.
Creating a child class ~ Routes #2:
class Main: class UserScore( Main ):
def __init__( self, name, age, location ): def __init__(self, name, age, location, score):
self.name = name super().__init__( name, age, location)
self.age = int( age ) self.score = int( score )
self.location = location
def checkAvg(self, list1 ):
def user_info(self): x = self.score / len(list1) * 100
print("Welcome," , self.name ) print("Results:", x)
print("You are:", self.age )
print("You live in:", self.location )

We are initializing the properties from the Superclass by using the super() function as
well as creating 1 new property for the child class.
Creating a child class ~ Routes #2:
class Main: class UserScore( Main ):
def __init__( self, name, age, location ): def __init__(self, name, age, location, score):
self.name = name super().__init__( name, age, location)
self.age = int( age ) self.score = int( score )
self.location = location
def checkAvg(self, list1 ):
def user_info(self): x = self.score / len( list1 ) * 100
print("Welcome," , self.name ) print("Results:", x )
print("You are:", self.age )
test_list = [4, 5, 5, 4, 3, 5, 5, 4]
print("You live in:", self.location )

user = UserScore("Josh", 25, "HCMC", 5)


user.checkAvg( test_list )
Output in Terminal

*Never use self outside of the class


Results: 5

You might also like