ALevel-CS Chapter 27

Object-Oriented Programming (OOP)

27.01 Concept of OOP

Key Terms

  • Object - an instance of a class
  • Attributes - the data items of a class (also sometimes referred to as fields)
  • Methods - the subroutines of a class
  • Encapsulation - combining data and subroutines into a class and restricting external access to the data
  • Class - a type that combines a data structure with the methods that operate on the data structure

Record

TYPE CarRecord 
    DECLARE VehicleID             : STRING
    DECLARE Registration          : STRING
    DECLARE DateOfRegistration    : DATE
    DECLARE EngineSize            : INTEGER
    DECLARE PurchasePrice         : CURRENCY
ENDTYPE

PROCEDURE UpdateRegistration(BYREF ThisCar : CarRecord, BYVALUE NewRegistration)
    ThisCar.Registration ← NewRegistration 
ENDPROCEDURE

Classes in OOP

  • OOP goes one step further and groups together the data structure and the subroutines that operate on the data items in this data structure. Such a group is called an object.
  • The data of an object are called attributes and the subroutines acting on the attributes are called methods.
  • The idea behind OOP is that attributes can only be accessed through methods.The direct path to the data is unavailable. Attributes are referred to as ‘private’. The methods to access the data are made available to programmers, so these are ‘public’. The feature of data being combined with the subroutines acting on this data is known as encapsulation.
  • To use an object, we first define an object type. An object type is called a class.
  • Classes are templates for objects. When a class type has been defined it can be used to create one or more objects of this class type. Therefore, an object is an instance of a class.
  • The first stage of writing an object-oriented program to solve a problem is to design the classes.

27.02 Designing classes and objects

Key Terms

  • Getter - a method to access its associated attribute
  • Constructor - a special type of method that is called to create a new object and initialise its attributes
  • Setter - a method to set the value of its associated attribute
  • Property - a class member that includes the attribute and also getter and setter method calls to access/set the attribute value

Designing classes and objects

  • Getter
    • Any data that is held about an object must be accessible, otherwise there is no point in storing it. We therefore need methods to access each one of these attributes. These methods are usually referred to as getters. They get an attribute of the object.
  • Constructor
    • When we first set up an object of a particular class, we use a constructor. A constructor instantiates the object and assigns initial values to the attributes.
  • Setter
    • Any attributes that might be updated after instantiation will need subroutines to update their values. These are referred to as setters.
  • Property
    • A property combines the attribute with its associated setter and/or getter

Designing classes and objects

When a car is manufactured it is given a unique vehicle ID that will remain the same throughout the car’s existence. The engine size of the car is fixed at the time of manufacture. The registration ID will be given to the car when the car is sold.


In our program, when a car is manufactured, we want to create a new car object. We need to instantiate it using the constructor. Any attributes that are already known at the time of instantiation can be set with the constructor. In our example, VehicleID and EngineSize can be set by the constructor. The other attributes are assigned values at the time of purchase and registration. So, we need setters for them.

27.03 Writing object-oriented code

Declaring a class

Attributes should always be declared as ‘Private’. This means they can only be accessed through the class methods. So that the methods can be called from the main program, they have to be declared as ‘Public’. There are other modifiers (such as ‘Protected’), but they are beyond the scope of this book.

Declaring a class in Python

Python

python property

Instantiating a class

To use an object of a class type in a program the object must first be instantiated. This means the memory space must be reserved to store the attributes.

ThisCar = Car("ABC1234", 2500)

Using a method

To call a method in program code, the object identifier is followed by the method identifier and the parameter list.

# sets the purchase price for an object ThisCar of class Car.
ThisCar.SetPurchasePrice(12000) 
ThisCar.PurchasePrice = 12000 # using properties
# gets and prints the vehicle ID for an object ThisCar of class Car.
print(ThisCar.GetVehicleID()) 
print(ThisCar.VehicleID) # using properties

27.04 Inheritance

Key Terms

  • Inheritance - all attributes and methods of the base class are copied to the subclass
  • Abstract class - a base class that is never used to create objects directly

Inheritance

The advantage of OOP is that we can design a class (a base class or a superclass) and then derive further classes (subclasses) from this base class. This means that we write the code for the base class only once and the subclasses make use of the attributes and methods of the base class, as well as having their own attributes and methods.

Example

  • A college library has items for loan.
  • The items are currently books and CDs.
  • Items can be borrowed for three weeks.
  • If a book is on loan, it can be requested by another borrower.

Declaring a base class and derived classes (subclasses) in Python

Instantiating a subclass

ThisBook = Book(Title, Author, ItemID) 
ThisCD = CD(Title, Artist, ItemID)

27.05 Polymorphism

Key Terms

  • Polymorphism - the method behaves differently for different classes in the hierarchy

Polymorphism

To include the additional attributes from the subclass, we need to declare the method again. Although the method in the subclass will have the same identifier as in the base class, the method will actually behave differently. This is known as polymorphism.

27.06 Garbage collection

Garbage collection

When objects are created they occupy memory. When they are no longer needed, they should be made to release that memory, so it can be re-used. If objects do not let go of memory, we eventually end up with no free memory when we try and run a program. This is known as ‘memory leakage’.

Memory management involves a private heap containing all Python objects and data structures. The management of the Python heap is performed by the interpreter itself. The programmer does not need to do any housekeeping.

27.07 Containment (aggregation)

Key Terms

  • Containment - a relationship in which one class has a component that is of another class type

Example