0% found this document useful (0 votes)
14 views72 pages

Object-Oriented Programming Basics

The document outlines a series of lectures on Object-Oriented Programming (OOP) focusing on inheritance, with specific examples in Python. It covers the creation of classes, methods, and the concepts of composition and aggregation, as well as the structure of inheritance in Python. Key topics include the use of the __init__() method and the distinction between inherited and overwritten methods.

Uploaded by

satah54172
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)
14 views72 pages

Object-Oriented Programming Basics

The document outlines a series of lectures on Object-Oriented Programming (OOP) focusing on inheritance, with specific examples in Python. It covers the creation of classes, methods, and the concepts of composition and aggregation, as well as the structure of inheritance in Python. Key topics include the use of the __init__() method and the distinction between inherited and overwritten methods.

Uploaded by

satah54172
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

Object-Oriented Programming

Inheritance

Anastasia Dimou
[Link]@[Link]
Lectures outline

30 Sep: Lecture 1: Object-Oriented Programming and UML


07 Oct: Lecture 2: Object-Oriented Programming with Python
21 Oct: Lecture 3: Association: Composition & Aggregation
28 Oct: Lecture 4: Inheritance
04 Nov: Lecture 5: GUIs for Python
18 Nov: Lecture 6: Equality
25 Nov: Lecture 7: Exceptions and Error Handling
02 Dec: Lecture 8: Unit Testing
09 Dec: Lecture 9: Data analytics
16 Dec: Lecture 10: Wrap up
(subject to small changes)

2
Object-Oriented Programming

What did we learn so far?


What we will learn today
Inheritance

3
Object-Oriented Programming

What did we learn so far?


OOP in Python
Association: Composition & Aggregation
Lists of Objects

4
Object-Oriented Programming

What did we learn so far?


OOP in Python
Association: Composition & Aggregation

5
class in Python instance in Python

to create a class, use the class keyword syntax: <object>=<class>()


syntax: class <name> :
example:
example: class Person():
class Person():
pass

p1=Person()
If we have a class with no content,
we use the pass statement.

6
class with method - self parameter & multiple parameters

Difference between function & method:


methods have a required parameter, conventionally named self
The self parameter should always be a parameter, even if we have multiple arguments.
syntax: def <name> (self,arg1,arg2,...,argN) :

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)

to call the attributes & methods of an instance,


we use the dot notation.
instantiate an object
class Person: syntax: <object>=<class>()
example: p1=Person()
def setName(self,x):
[Link] = x (i) call a method of the class
syntax: <object>.<method>()
example: [Link]()
def setSurname(self,x,y):
[Link](x) (ii) call an attribute of the class
[Link] = y syntax: <object>.<method>()
example: [Link]

9
Object-Oriented Programming

What did we learn so far?


OOP in Python
Association: Composition & Aggregation

10
objA1

Composition attrA1 ...

attrA2

the class A is associated with the class B objB1

attrB1 ...

an instance of class A, e.g., objA1, attrB2 ...

is associated with an instance of class B, e.g., objB1 attrB3 ...

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

the class A is associated with the class B objB1

attr1 ...

an instance of class A, e.g., objA1, attr2 ...


is associated with an instance of class B, e.g., objB1 attr3 ...

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

classA acts as the container of classB classA refers to classB


delete classA → deletes classB delete classA → does NOT delete classB

class classB(): pass class classB(object): pass


class classA(object):
class classA(object):
def __init__(self, b):
def __init__(self, b):
self.b = b
self.b = B()
b = classB()
a = classA(b)

13
Object-Oriented Programming

What did we learn so far?


What we will learn today
Inheritance

14
Transportation
Vehicle

Land Vehicle Air Vehicle Water Vehicle

Car Motorcycle Bicycle Train Airplane Helicopter Boat Submarine

Ferry Boat Sailing Boat kayak canoe

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

Land Vehicle Air Vehicle Water 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

syntax: childClass (parentClass): class Car(LandVehicle):


pass
class Vehicle(): class Motorcycle(LandVehicle):
pass
pass
class Bicycle(LandVehicle):
class LandVehicle(Vehicle):
pass pass
class Train(LandVehicle):
class AirVehicle(Vehicle): pass
pass
class Airplane(AirVehicle):
pass
class WaterVehicle(Vehicle):
pass

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!")

v1 = Vehicle() → "A new vehicle was created!"

20
Inheritance: __init__()

class Vehicle():
def __init__(self):
print("A new vehicle was created!")

class LandVehicle(Vehicle):
pass

v1 = Vehicle() → "A new vehicle was created!"


v2 = LandVehicle()

21
Inheritance: __init__()

class Vehicle(): Is there an __init__ method?


def __init__(self):
print("A new vehicle was created!") No

Is there a Parent Class?


class LandVehicle(Vehicle):
pass Yes!

v1 = Vehicle() → "A new vehicle was created!" Which is the Parent Class?
v2 = LandVehicle()
Check in the parenthesis!

Does the Parent Class have an


__init__() method?

Yes!

22
Inheritance: __init__()
class Vehicle():
def __init__(self):
print("A new vehicle was created!")

class LandVehicle(Vehicle):
pass

class Car(LandVehicle):
pass

v1 = Vehicle() → "A new vehicle was created!"


v2 = LandVehicle() → "A new vehicle was created!"
v3 = Car() → "A new vehicle was created!"

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!")

Is there the … method?


class Car(LandVehicle):
Yes!
def __init__(self,name):
[Link](name)

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 Vehicle(): syntax: super(<ChildClass>,self).__init__()


def __init__(self): <ChildClass> → Which child class calls the super
print("A new vehicle was created!") Our way to refer to the Parent Class of a Child Class
def setOwner(self,name):
when to use: to inherit the functionality of the parent class,
[Link] = name
but we also want to do something extra
print("The owner is set to " + [Link])

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 Vehicle(): syntax: super(<ChildClass>,self).__init__()


def __init__(self): <ChildClass> → Which child class calls the super
print("A new vehicle was created!") Our way to refer to the Parent Class of a Child Class
def setOwner(self,name):
v4 = AirVehicle()
[Link] = name
print("The owner is set to " + [Link])

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 Vehicle(): syntax: super(<ChildClass>,self).__init__()


def __init__(self): <ChildClass> → Which child class calls the super
print("A new vehicle was created!") Our way to refer to the Parent Class of a Child Class
def setOwner(self,name):
v4 = AirVehicle()
[Link] = name
→ "I am cool because I still do my own stuff
print("The owner is set to " + [Link]) but I also inherit from my parent class!"

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

class Vehicle(): syntax: super(<ChildClass>,self).__init__()


def __init__(self): <ChildClass> → Which child class calls the super
print("A new vehicle was created!") Our way to refer to the Parent Class of a Child Class
def setOwner(self,name):
v4 = AirVehicle()
[Link] = name
→ "I am cool because I still do my own stuff
print("The owner is set to " + [Link]) but I also inherit from my parent class!"
→ "A new vehicle was created!"
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__()

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] = name super(AirVehicle, self).__init__("Bob")

class AirVehicle(Vehicle): [Link](pilot)


def __init__(self,name,pilot,seats,passengers): [Link](seats)
super(AirVehicle, self).__init__(name) [Link](passengers)
[Link](pilot)
[Link](seats) v4
def __init__(self,"Bob"):
[Link](passengers)
[Link]("Bob")
def setPilot(self,pilot):

[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] = name super(AirVehicle, self).__init__("Bob")

class AirVehicle(Vehicle): [Link](pilot)


def __init__(self,name,pilot,seats,passengers): [Link](seats)
super(AirVehicle, self).__init__(name) [Link](passengers)
[Link](pilot)
[Link](seats)

[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] = name super(AirVehicle, self).__init__("Bob")

class AirVehicle(Vehicle): [Link](pilot)


def __init__(self,name,pilot,seats,passengers): [Link](seats)
super(AirVehicle, self).__init__(name) [Link](passengers)
[Link](pilot)
[Link](seats) v4
def setPilot(self,"Alice"):
[Link](passengers)
[Link] = "Alice"
def setPilot(self,pilot):

[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] = name super(AirVehicle, self).__init__("Bob")

class AirVehicle(Vehicle): [Link](pilot)


def __init__(self,name,pilot,seats,passengers): [Link](seats)
super(AirVehicle, self).__init__(name) [Link](passengers)
[Link](pilot)
[Link](seats)

[Link](passengers)
def setPilot(self,pilot):

[Link] = pilot
def setSeats(self,seats):
v4
[Link] = seats

def setPassengers(self,passengers): owner "Bob"


[Link] = passengers pilot "Alice"
def freeSeats(self):
seats 96
return [Link] - [Link]

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] = name super(AirVehicle, self).__init__("Bob")

class AirVehicle(Vehicle): [Link](pilot)


def __init__(self,name,pilot,seats,passengers): [Link](seats)
super(AirVehicle, self).__init__(name) [Link](passengers)
[Link](pilot)
[Link](seats) v4
def setPassengers(self,74):
[Link](passengers)
[Link] = 74
def setPilot(self,pilot):

[Link] = pilot v4
def setSeats(self,seats):
owner "Bob"
[Link] = seats

def setPassengers(self,passengers):
pilot "Alice"

[Link] = passengers seats 96


def freeSeats(self):
passengers 74
return [Link] - [Link]

61
Inheritance - call a method

obj = classA(...)
[Link](...)
Is there the … method?
No

class... has Yes Is there a Parent Class?


execute methodA(...)
methodA(...)? Yes!
Which is the Parent Class?
Yes No Check in the parenthesis!

Does the Parent Class have an ... method?


class... has
parent class? Yes!

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

Inherited Methods Overwritten Methods Additional Methods


class ParentClassB: class ParentClassC:
class ParentClassA: def __init__(self):
def __init__(self):
def __init__(self): pass pass
def methodB(self): def methodC1(self):
pass
pass pass
def methodA(self):
pass class ChildClassB(ParentClassB): class ChildClassC(ParentClassB):
def __init__(self): def __init__(self):
pass pass
class ChildClass(ParentClassA): def methodB(self): def methodC1(self):
pass pass
pass
def methodC2(self):
pass

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)

class Person: x = getattr(Person, ‘name')


name = "Bob"
age = "16"

65
Attributes – setattr()

sets the value of the specified attribute of the specified object


syntax: setattr(object,attribute,value)
attribute: the attribute’s name to set the value from
value: the value you want to give the specified attribute

class Person: x = getattr(Person, 'name')


name = "Bob" Y = setattr(Person, 'age', 40)
age = "16"

66
Attributes – hasattr()

sets the value of the specified attribute of the specified object


syntax: hasattr(object,attribute)
attribute: the attribute’s name to check if it exists

class Person: x = getattr(Person, 'name')


name = "Bob" y = setattr(Person, 'age', 40)
age = "16" Z = hasattr(Person, 'age')

67
Attributes – delattr()

sets the value of the specified attribute of the specified object


syntax: delattr(object,attribute)
attribute: the attribute’s name to set the value from

class Person: x = getattr(Person, 'name')


name = "Bob" setattr(Person, 'age', 40)
age = "16" y = hasattr(Person, 'age')
delattr(Person, 'age')

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

returns True if the object is a subclass of another object, otherwise False


syntax: issubclass(object,subclass)

class Person: p=Person()


name = "Bob" z=isinstance(p,Person)
age = "16"
x=issubclass(Student,Pereson)
class Student(Person): y=issubclass(Person,Student)
pass

70
Object-Oriented Programming

Inheritance
Inherited Methods
Overwritten Methods
Additional Methods
Built-in Functions

71
Object-Oriented Programming
Inheritance

Anastasia Dimou
[Link]@[Link]

You might also like