Object-Oriented Programming Basics
Object-Oriented Programming Basics
Inheritance
Anastasia Dimou
[Link]@[Link]
Lectures outline
2
Object-Oriented Programming
3
Object-Oriented Programming
4
Object-Oriented Programming
5
class in Python instance in Python
p1=Person()
If we have a class with no content,
we use the pass statement.
6
class with method - self parameter & multiple parameters
class Person:
def setName(self):
pass
7
call the attributes & methods of an instance (inside)
to call the attributes & methods of an instance, we use the dot notation.
syntax: self.<attribute> & self.<method>
class Person:
def setName(self,x):
[Link] = x
def setSurname(self,x,y):
[Link](x)
[Link] = y
8
call the attributes & methods of an instance (outside)
9
Object-Oriented Programming
10
objA1
attrA2
attrB1 ...
BUT
if we delete the instance of class A, e.g., objA1,
then the associated instance of class B, e.g., objB1, is ALSO deleted.
when to use: if the existence of the one object DEPENDS on the other
example: A house object consists of room objects,
but the room objects can NOT exist without the house object
11
objA1
Aggregation attr1 ..
attr2
attr1 ...
BUT
if we delete the instance of class A, e.g., objA1,
then the instance of class B, e.g., objB1, is NOT deleted as well
when to use: if the existence of the one object does NOT DEPEND on the other
example: A person object owns a house object,
but the house object can exist without the person object
12
Composition Vs Aggregation
Composition Aggregation
13
Object-Oriented Programming
14
Transportation
Vehicle
15
Inheritance in Python
to create a child class that inherits the functionality from a parent class,
include the parent class as a parameter when creating the child class:
syntax: childClass (parentClass):
class Vehicle():
pass
Vehicle
16
Inheritance in Python
to create a child class that inherits the functionality from a parent class,
include the parent class as a parameter when creating the child class:
syntax: childClass (parentClass):
class Vehicle():
pass
class LandVehicle(Vehicle):
pass
class AirVehicle(Vehicle):
pass
Vehicle
class WaterVehicle(Vehicle):
pass
Land Vehicle Air Vehicle Water Vehicle
17
Air Vehicle
Inheritance in Python
Airplane Helicopter
18
Object-Oriented Programming
Inheritance
Inherited Methods
Overwritten Methods
Additional Methods
Built-in Functions
19
Inheritance: __init__()
class Vehicle():
def __init__(self):
print("A new vehicle was created!")
20
Inheritance: __init__()
class Vehicle():
def __init__(self):
print("A new vehicle was created!")
class LandVehicle(Vehicle):
pass
21
Inheritance: __init__()
v1 = Vehicle() → "A new vehicle was created!" Which is the Parent Class?
v2 = LandVehicle()
Check in the parenthesis!
Yes!
22
Inheritance: __init__()
class Vehicle():
def __init__(self):
print("A new vehicle was created!")
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
23
Inheritance: inherit methods
class Vehicle(): [Link]("Bob")
def __init__(self):
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
24
Inheritance: inherit methods
class Vehicle(): [Link]("Bob")
def __init__(self): v1
def setOwner(self,"Bob"):
print("A new vehicle was created!")
[Link] = "Bob"
def setOwner(self,name): print("The owner is set to " + "Bob")
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
25
Inheritance: inherit methods
class Vehicle(): [Link]("Bob") → "The owner is set to Bob"
def __init__(self):
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
26
Inheritance: inherit methods
class Vehicle(): [Link]("Bob") → "The owner is set to Bob"
def __init__(self):
[Link]("Alice")
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
27
Inheritance: inherit methods
class Vehicle(): [Link]("Bob") → "The owner is set to Bob"
def __init__(self):
[Link]("Alice")
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
Is there the … method?
class LandVehicle(Vehicle):
No
pass
Is there a Parent Class?
class Car(LandVehicle): Yes!
pass Which is the Parent Class?
Check in the parenthesis!
v1 = Vehicle()
Does the Parent Class have a ... method?
v2 = LandVehicle()
v3 = Car() Yes!
28
Inheritance: inherit methods
class Vehicle(): [Link]("Bob") → "The owner is set to Bob"
def __init__(self):
[Link]("Alice")→ "The owner is set to Alice"
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
29
Inheritance: inherit methods
class Vehicle(): [Link]("Bob") → "The owner is set to Bob"
def __init__(self):
[Link]("Alice")→ "The owner is set to Alice"
print("A new vehicle was created!")
[Link]("Carol")→ "The owner is set to Carol"
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
30
Inheritance in Python
to create a child class that inherits the functionality from a parent class,
include the parent class as a parameter when creating the child class:
syntax: childClass (parentClass):
Inherited Methods
class ParentClass:
def __init__(self):
pass
def method(self):
pass
class ChildClass(ParentClass):
pass
31
Object-Oriented Programming
Inheritance
Inherited Methods
Overwritten Methods
Additional Methods
Built-in Functions
32
Inheritance: overwrite __init__()
class Vehicle(): [Link]("Bob") → "The owner is set to Bob"
def __init__(self):
[Link]("Alice")→ "The owner is set to Alice"
print("A new vehicle was created!")
[Link]("Carol")→ "The owner is set to Carol"
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
pass
class Car(LandVehicle):
pass
v1 = Vehicle()
v2 = LandVehicle()
v3 = Car()
33
Inheritance: overwrite __init__()
class Vehicle():
def __init__(self):
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
pass
34
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
pass
35
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
Is there the … method?
print("The owner is set to " + [Link])
Yes!
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
pass
36
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()→ "A new LAND vehicle was created!"
print("A new vehicle was created!")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
pass
37
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()→ "A new LAND vehicle was created!"
print("A new vehicle was created!") v3 = Car(”Carol")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
def __init__(self,name):
[Link](name)
38
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()→ "A new LAND vehicle was created!"
print("A new vehicle was created!") v3 = Car("Carol")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
39
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()→ "A new LAND vehicle was created!"
print("A new vehicle was created!") v3 = Car("Carol")
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
def __init__(self,name):
[Link](name)
40
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()→ "A new LAND vehicle was created!"
print("A new vehicle was created!") v3 = Car("Carol")
def setOwner(self,name):
Is there the … method?
[Link] = name
No
print("The owner is set to " + [Link])
Is there a Parent Class?
class LandVehicle(Vehicle): Yes!
def __init__(self): Does the Parent Class have an ... method?
print("A new LAND vehicle was created!") No
Is there a Parent Class?
class Car(LandVehicle):
Yes!
def __init__(self,name):
Does the Parent Class have a ... method?
[Link](name)
Yes!
41
Inheritance: overwrite __init__()
class Vehicle(): v1 = Vehicle() → "A new vehicle was created!"
def __init__(self): v2 = LandVehicle()→ "A new LAND vehicle was created!"
print("A new vehicle was created!") v3 = Car("Carol") → "The owner is set to Carol"
def setOwner(self,name):
[Link] = name
print("The owner is set to " + [Link])
class LandVehicle(Vehicle):
def __init__(self):
print("A new LAND vehicle was created!")
class Car(LandVehicle):
def __init__(self,name):
[Link](name)
42
Inheritance in Python
If you want to create a child class that inherits the functionality from a parent class,
we should include the parent class as a parameter when creating the child class:
syntax: childClass (parentClass):
Overwritten Methods
Inherited Methods class ParentClassB:
def __init__(self):
class ParentClassA: pass
def __init__(self): def methodB(self):
pass
pass
def methodA(self): class ChildClassB(ParentClassB):
pass def __init__(self):
pass
def methodB(self):
class ChildClass(ParentClassA): pass
pass
43
Object-Oriented Programming
Inheritance
Inherited Methods
Overwritten Methods + Super
Additional Methods
Built-in Functions
44
Inheritance: super() calls same method but in parent class
class AirVehicle(Vehicle):
def __init__(self):
print("I am cool because still do my own stuff
but I also inherit from my parent class!")
super(AirVehicle, self).__init__()
45
Inheritance: super()
class AirVehicle(Vehicle):
def __init__(self):
print("I am cool because I still do my own stuff
but I also inherit from my parent class!")
super(AirVehicle, self).__init__()
46
Inheritance: super()
class AirVehicle(Vehicle):
def __init__(self):
print("I am cool because I still do my own stuff
but I also inherit from my parent class!")
super(AirVehicle, self).__init__()
47
Inheritance: super()
48
Object-Oriented Programming
Inheritance
Inherited Methods
Overwritten Methods
Additional Methods
Built-in Functions
49
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name):
[Link](name)
def setOwner(self,name):
[Link] = name
class AirVehicle(Vehicle):
def __init__(self,name,pilot,seats,passengers):
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
50
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v3
def __init__(self,"Bob"):
[Link](name)
[Link]("Bob")
def setOwner(self,name):
[Link] = name
class AirVehicle(Vehicle):
def __init__(self,name,pilot,seats,passengers):
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
51
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v3
def __init__(self,"Bob"):
[Link](name)
[Link]("Bob")
def setOwner(self,name):
[Link] = name
v3
class AirVehicle(Vehicle): def setOwner(self,"Bob"):
def __init__(self,name,pilot,seats,passengers): [Link] = name
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
52
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name)
def setOwner(self,name):
[Link] = name
class AirVehicle(Vehicle):
def __init__(self,name,pilot,seats,passengers):
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
53
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name)
def setOwner(self,name):
[Link] = name
class AirVehicle(Vehicle):
def __init__(self,name,pilot,seats,passengers):
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
54
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name)
def setOwner(self,name):
[Link] = name
class AirVehicle(Vehicle):
def __init__(self,name,pilot,seats,passengers):
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
55
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name)
def setOwner(self,name):
[Link] = name
class AirVehicle(Vehicle):
def __init__(self,name,pilot,seats,passengers):
super(AirVehicle, self).__init__(name)
[Link](pilot)
[Link](seats)
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
def freeSeats(self):
return [Link] - [Link]
56
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name) v4
def setOwner(self,name):
def __init__(self,"Bob","Alice",96,74):
[Link] = pilot v4
def setOwner(self,"Bob"):
def setSeats(self,seats):
[Link] = "Bob"
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
v4
def freeSeats(self):
return [Link] - [Link] owner "Bob"
57
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name) v4
def setOwner(self,name):
def __init__(self,"Bob","Alice",96,74):
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
[Link] = seats
def setPassengers(self,passengers):
[Link] = passengers
v4
def freeSeats(self):
return [Link] - [Link] owner "Bob"
58
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name) v4
def setOwner(self,name):
def __init__(self,"Bob","Alice",96,74):
[Link] = pilot v4
def setSeats(self,96):
def setSeats(self,seats):
[Link] = 96
[Link] = seats
def setPassengers(self,passengers):
v4
[Link] = passengers
owner "Bob"
def freeSeats(self):
return [Link] - [Link] pilot "Alice"
59
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name) v4
def setOwner(self,name):
def __init__(self,"Bob","Alice",96,74):
[Link](passengers)
def setPilot(self,pilot):
[Link] = pilot
def setSeats(self,seats):
v4
[Link] = seats
60
class Vehicle(): v3 = Vehicle("Bob")
def __init__(self,name): v4 = AirVehicle("Bob","Alice",96,74)
[Link](name) v4
def setOwner(self,name):
def __init__(self,"Bob","Alice",96,74):
[Link] = pilot v4
def setSeats(self,seats):
owner "Bob"
[Link] = seats
def setPassengers(self,passengers):
pilot "Alice"
61
Inheritance - call a method
obj = classA(...)
[Link](...)
Is there the … method?
No
62
Inheritance in Python
If you want to create a child class that inherits the functionality from a parent class,
we should include the parent class as a parameter when creating the child class:
syntax: childClass (parentClass):
63
Object-Oriented Programming
Inheritance
Inherited Methods
Overwritten Methods
Additional Methods
Built-in Functions
64
Attributes – getattr()
returns the value of the specified attribute from the specified object
syntax: getattr(object,attribute,default)
attribute: the attribute’s name to get the value from
default: the value to return if the attribute does not exist (optional)
65
Attributes – setattr()
66
Attributes – hasattr()
67
Attributes – delattr()
68
Attributes – isinstance()
returns True if the specified object is of the specified type, otherwise False
syntax: isinstance(object,type)
type: a type or a class
x=isinstance(5, int)
class Person: Y=isinstance("Hello",(float,int,str,list))
name = "Bob"
age = "16" p=Person()
z=isinstance(p,Person)
69
Attributes – issubclass()
70
Object-Oriented Programming
Inheritance
Inherited Methods
Overwritten Methods
Additional Methods
Built-in Functions
71
Object-Oriented Programming
Inheritance
Anastasia Dimou
[Link]@[Link]