top of page

Use of object oriented techniques. (2.2.1)

  • 20p13280
  • Dec 16, 2025
  • 6 min read

Updated: Dec 17, 2025

This post covers the following from the specification (2.2.1):

(a) Programming constructs: sequence, iteration, branching.

(b) Recursion, how it can be used and compares to an iterative approach.

(c) Global and local variables.

(d) Modularity, functions and procedures, parameter passing by value and by reference.

(e) Use of an IDE to develop/debug a program.

(f) Use of object oriented techniques.


(the bold init within these notes is due to wix applying markdown formatting and I can't remove it so assume that it's underscore(_) underscore(_) init underscore(_) underscore(_)).


Object Oriented Programming (OOP) is a type of bottom-up programming that attempts to model real world scenarios and simulations using objects and classes. OOP means that code is written with modularity in mind due to the use of classes. Classes being blueprints for objects, so defining their methods and attributes.


Classes & Objects

A class is a blueprint for an object, it contains all the attributes and methods that an object will contain once it is created, but the actual instance of the class is called an object. An object is self-contained and holds all the attributes and contains all the methods that are part of the class. As they are self-contained it means that data integrity and security can be done effectively as a class object only stores info for that class object. A class is also referred to as a data type, like a string or an integer. Within a class you can also show relationships between objects by setting up attributes that will have values of other class objects. 


When a new object of a class is created, it's called instanciation and this is also when the constructor is called to define the class objects attributes.


For a song class with parameters of artist name, song name and song length seconds, instanciating it looks something like this:


grapes_song = Song("James Marriott", "Grapes", 264) 
food_poisoning_song = Song("James Marriott", "Food Poisoning", 210)
flicker_song = Song("Niall Horan", "Flicker",500)

Within OCR ERL/Pseudocode it would be:

grapes_song = new Song("James Marriot", "Grapes", 264)
food_poisoning_song = new Song("James Marriott", "Food Poisoning",210)
flicker_song = new Song("Niall Horan", "Flicker",500)

The grapes_song variable now stores the class object of the song Grapes by James Marriott with a length of 264 seconds. The same with the other 2 (food poisoning and flicker with their own values for song name, song artist and song duration in seconds).


Attribute

An attribute is what variables are referred to in OOP. Unlike procedural variables are not global or local, they are either public or private. Public meaning that it is accessible everywhere that class object is referenced, private meaning that it's only accessible within the class reference. Typically each attribute will have a getter and setter method so that the value can be retrieved and updated (mroe on methods below).


For a song class, attributes (defined in the class that won't change and have static values) would look like this:


class Song:
    artist_name = "James Marriott"
    song_name = "Grapes"
    song_length_seconds = 264

Methods

A method is a subroutine on a class, typically they have access to the class itself. Methods are accessible from the within a class object, on the class object variable/reference or if it’s a static method then anywhere. Methods are essentially subroutines for classes, they run code and can return values but don’t need to. In languages like python they require that a self instance be the first variable.


One method that a class MUST HAVE is a Constructor. A constructor is a method on a class that is called when it is instanciated (created), a constructor will have parameters that will be used to set the value of any attributes of the class. A constructor can also have methods run in it and other code run in it, so for example if a song class is created a method that might be called in the constructor is one to upload it to spotify. In some languages, like attributes, class methods can be public or private too (with some like Java also adding in a protected keyword too).


For a song class, a constructor would look something like this:


class Song:
    artist_name = ""
    song_name = ""
    song_length_seconds = 0

    def __init__(self,artist_name,song_name,song_length_seconds):  
   	  self.artist_name = artist_name
      self.song_name = song_name
      self.song_length_seconds = song_length_seconds

Encapsulation

Encapsulation is where attributes (and methods sometimes) are bundled together into one and they have a keyword assigned to them, either public or private (or in languages like Java, protected). This is about changing the visibility of data within a program and a class object itself. This is what allows for data integrity and security, and also preventing attributes from being accidentally overriden which could cause logic errors in a program.


Inheritance

This is where a new class is created which derives from a base/super class. The new class will copy over all the same attributes and methods that were in the super class, but this allows for additional functionality to be added through the use of new attributes and methods. The new class is referred to as the child class, the class it derives from is referred to as either the base class, super class or parent class.


For example, a song might look like this (assume each attribute has getters and setters):


class Song:
    artist_name = ""
    song_name = ""
    song_length_seconds = 0

    def __init__(self,artist_name,song_name,song_length_seconds):        
        self.artist_name = artist_name
        self.song_name = song_name
        self.song_length_seconds = song_length_seconds

    def play(self):
        print(f"Played {self.song_name} by {self.artist_name}")

A streaming platform like spotify will need a way to store streams, so they may make a child class of Song which can include attributes and methods for streaming, seen here:


class StreamedSong(Song):
    song_streams = 0

    def __init__(self,artist_name,song_name,song_length_seconds,song_streams=0):
        super.__init__(artist_name,song_name,song_length)
        self.song_streams = song_streams

    def incriment_streams(self):
        self.streams += 1
        print("Incrimented streams by +1")

                 

Here the StreamedSong class has inherited from the Song class, creating a new attribute called "song_streams" which will track the amount of times a song is streamed, also creating a new method called incriment_streams() which will increment the stream count by 1. However, a platform may want to increment streams when the play() method is called, and this is where another construct called polymorphism comes in.


Polymorphism

Polymorphism comes from the words "poly" and "morphism", "poly" meaning many and "morphism" meaning "form" or "shape". This is the process of which methods within a super class can be overridden from a child class to give different functionality. The method will still have the same name but just have the updated functionality by overriding it. To get the original functionality within the method you would call "super()" from within the method to run the existing method from the parent class. In the case of the Song class with the streaming service, the play method can be overridden to provide new functionality, in this case incrimenting streams by 1. If this were to be applied to the StreamedSong class previously, it would look like this:


class StreamedSong(Song):
    song_streams = 0

    def init(self,artist_name,song_name,song_length_seconds,song_streams=0):     
        super.__init__(artist_name,song_name,song_length)
        self.song_streams = song_streams

    def play(self):
        super().play(self) # Run code from super class
        self.streams += 1 # Polymorphism: Increment streams by 1

    def incriment_streams(self):
        self.streams += 1
        print("Incrimented streams by +1")

There is another type of polymorphism known as static polymorphism, this is where a class contains multiple methods with the same name but vary within the parameters, for example within a Song class there may be 2 types of buy().


def buy(self,cash_payed,wants_recipt,store_location)
def buy(self,card_number,address)

These both share the same name, however they have different parameters and different functionality inside, allowing them to share the name without an error. This is known as method overloading, it's not possible within procedural programming.


Class Diagrams

To represent classes, class diagrams are used. These are 3 boxes per class, the top box containing the class name, the second containing the class attributes and the type that they take. The third box contains contains all the methods, showing the parameters they take and the type of each parameter. For inheritence, an arrow is drawn going up from the child class to the base class to indicate that it's a child class and inheritance has taken place. There is something called the Unified Modeling Language (not 100% sure if it's on-spec) that is the standard for creating class diagrams (https://drive.google.com/file/d/1dmNMxVHo0ZwA8fCat6JAz7nkApn_HTJW/view).

Before each attribute or method, a + or - symbol needs to be present to indicate either public(+) or private(-).


This is an example of a class diagram of an Animal super class with 3 types of animal child classes:


OOP vs Procedural

OOP

Procedural

Easy to re-use code as each class is a blueprint for an object and can be instanciated and destroyed very easily. Automatically written to be modular.

Harder to re-use code as code has to be written with modularity in mind.

OOP will use larger amounts of memory due to the overhead from storing each individual classes attributes, types, etc.

Procedural uses less memory as it doesn't have to store multiple instances of variables for classes. (This is assuming they are both solving the same problem)

Handling errors is easier, as everything is self-contained by nature so if an error occurs it's typically limited to one class or one metod within a class. This also makes debugging easier.

Handing errors is hader as subroutine calls can be interlinked, especially with recursion and this makes debugging and handling errors harder than in OOP.

Better used for large-scale projects and real-world application projects.

Better used for smaller-scale proejcts and non-real-world applications.

Keywords for variables and subroutines are different, refered to as attributes and methods.

Subroutines and methods are refered to as subroutines and methods.

Attributes can either be public or private in-terms of visibiity.

Visibility is down to scope, variables can either be public or private.



 
 
 

Recent Posts

See All
Data Structures - Linked Lists (1.4.2)

This post covers the following topics: Linked Lists Linked Lists Linked lists are the most common type of dynamic data structure, as they are dynamic they can grow to any size that is required. Their

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Project Volt Logo
bottom of page