20. Further Programming
1. Basics of OOP
bject-oriented programming is organising the code behind programs to reflect the behaviours and
O
characteristics of objects in the real world
Key terms:
● Properties/attributes - the data items/attributes/characteristics and data types defined in a class
● Methods - the procedures/functions/instructions in a class, which implement the behaviours of
the object and act on the properties/attributes
● Class - a blueprint to create objects from, which defines the attributes and methods
● Object - a specific instance that implements the class
● Instance/instantiation - an occurrence of an object created in memory
ote: no memory is allocated when a class is defined, but objects are allocated memory space whenever
N
they are created/instantiated
constructor method is the method that is called automatically when an object is created, used to
A
initialise an object from a class
● In Python, it is the init method
Encapsulation:
● Wrapping properties/attributes and methods into a single entity (the class/object) to ensure
sensitive data is protected/hidden from users
● Attributes should only be accessed (retrieved/modified) using methods that are contained
(encapsulated) within the class definition
Getter and setter methods:
● Data should never be accessed directly e.g. using player1.name()
● Instead, getter and setter methods must be provided, encapsulated within the class definition,
that will allow access to return/update attributes
● Getter methods e.g. getName() return the value of an attribute/property
● Setter methods let you change/update the value of an attribute/property
OOP is useful because:
● Modularity - code can be broken down into smaller, more manageable pieces, making it easier to
understand/test/maintain, and better collaboration as different people can work on each module
● Reusability - many objects can be defined from the same class, saving development time
● Encapsulation/data hiding - data can be protected within classes with external access restricted
● Inheritance/polymorphism - classes can be derived from existing classes, saving development
time, and polymorphism allows for greater flexibility
, 2a. Inheritance
Inheritance:
● Inheritance is when a class (subclass) is derived from another class (superclass/parent class), and
the methods and properties/attributes of the superclass are made available to the derived class
● The subclass may also have its own additional attributes/methods (as well as the ones inherited)
● The attributes/methods derived from the superclass can also be extended/modified/overridden
● On diagrams, the arrow points towards the superclass
● In Python, the subclass is created as follows:
class SwimmingBird(Bird):
def __init__(self, c, a):
super(SwimmingBird, self).__init__(c,a)
Polymorphism & overriding:
● When methods with the same name behave differently in derived/inherited classes/subclasses
● A way to do this is using overriding - when a method with the same identifier/name in the
superclass is redefined or extended in a subclass
2b. Access Modifiers
Access modifiers:
● Way of making attributes and methods private, public or protected
● By default, attributes/methods are public(+)andcan be accessed outside of the class they are in
● Private(-)attributes and methods are not availablethroughout the entire program and are only
accessible using (getter/setter) methods within the class - done to prevent unauthorised access to
data/keep data hidden and enforce encapsulation
○ In Python, attributes/methods are made private using a double underscore, as follows:
class Bird:
def __init__(self, colour, age):
self.__featherColour = colour
self.age = age
def __fly(self):
print(“Bird flying”)
In above e.g. featherColour and fly method would be private, but age would be public
● Protected(#)attributes/methods are used in inheritance,and they are only available to the class
they are in and the classes that inherit it, but not from anywhere else outside those classes
○ In Python, one underscore is used to make attributes/methods protected, as follows:
class Animal:
def __init__(self, s, n):
self._state = s
self._size = n
1. Basics of OOP
bject-oriented programming is organising the code behind programs to reflect the behaviours and
O
characteristics of objects in the real world
Key terms:
● Properties/attributes - the data items/attributes/characteristics and data types defined in a class
● Methods - the procedures/functions/instructions in a class, which implement the behaviours of
the object and act on the properties/attributes
● Class - a blueprint to create objects from, which defines the attributes and methods
● Object - a specific instance that implements the class
● Instance/instantiation - an occurrence of an object created in memory
ote: no memory is allocated when a class is defined, but objects are allocated memory space whenever
N
they are created/instantiated
constructor method is the method that is called automatically when an object is created, used to
A
initialise an object from a class
● In Python, it is the init method
Encapsulation:
● Wrapping properties/attributes and methods into a single entity (the class/object) to ensure
sensitive data is protected/hidden from users
● Attributes should only be accessed (retrieved/modified) using methods that are contained
(encapsulated) within the class definition
Getter and setter methods:
● Data should never be accessed directly e.g. using player1.name()
● Instead, getter and setter methods must be provided, encapsulated within the class definition,
that will allow access to return/update attributes
● Getter methods e.g. getName() return the value of an attribute/property
● Setter methods let you change/update the value of an attribute/property
OOP is useful because:
● Modularity - code can be broken down into smaller, more manageable pieces, making it easier to
understand/test/maintain, and better collaboration as different people can work on each module
● Reusability - many objects can be defined from the same class, saving development time
● Encapsulation/data hiding - data can be protected within classes with external access restricted
● Inheritance/polymorphism - classes can be derived from existing classes, saving development
time, and polymorphism allows for greater flexibility
, 2a. Inheritance
Inheritance:
● Inheritance is when a class (subclass) is derived from another class (superclass/parent class), and
the methods and properties/attributes of the superclass are made available to the derived class
● The subclass may also have its own additional attributes/methods (as well as the ones inherited)
● The attributes/methods derived from the superclass can also be extended/modified/overridden
● On diagrams, the arrow points towards the superclass
● In Python, the subclass is created as follows:
class SwimmingBird(Bird):
def __init__(self, c, a):
super(SwimmingBird, self).__init__(c,a)
Polymorphism & overriding:
● When methods with the same name behave differently in derived/inherited classes/subclasses
● A way to do this is using overriding - when a method with the same identifier/name in the
superclass is redefined or extended in a subclass
2b. Access Modifiers
Access modifiers:
● Way of making attributes and methods private, public or protected
● By default, attributes/methods are public(+)andcan be accessed outside of the class they are in
● Private(-)attributes and methods are not availablethroughout the entire program and are only
accessible using (getter/setter) methods within the class - done to prevent unauthorised access to
data/keep data hidden and enforce encapsulation
○ In Python, attributes/methods are made private using a double underscore, as follows:
class Bird:
def __init__(self, colour, age):
self.__featherColour = colour
self.age = age
def __fly(self):
print(“Bird flying”)
In above e.g. featherColour and fly method would be private, but age would be public
● Protected(#)attributes/methods are used in inheritance,and they are only available to the class
they are in and the classes that inherit it, but not from anywhere else outside those classes
○ In Python, one underscore is used to make attributes/methods protected, as follows:
class Animal:
def __init__(self, s, n):
self._state = s
self._size = n