1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 7. Object Oriented Programming


Lesson Objective

  • Understand the basics of Object Oriented Programming.
  • Define a Class.
  • Set attribute and create methods.
  • Understand the basics of Encapsulation.
  • Instantiate an Object.
  • Use Constructors to create objects and set values.

Lesson Notes

Procedural Programming

Procedural programming is a style of programming that divides a program into a set of subroutines.

Data is stored in variables and functions operate on that data.

This style of programming is simple and straightforward, but as programs grow, it can become difficult to manage. Procedural code can become difficult to understand and maintain.

Object-oriented programming (OOP) came to solve this problem.


Object Oriented Programming

Groups of related variables and subroutines are grouped together into classes.

A class specifies all the attributes and methods.

Classes become blueprints for objects and define how objects will look and what they do.

main.py

class Student:
  def __init__(self):
    self.name = None
    self.age = None

def main():
  student1 = Student()
  student2 = Student()
  student1.name = "Bob"
  student1.age = 12
  student2.name = "Anne"
  student2.age = 435
  print(student1.name)

main()    

Cmd Output:

Ahmed

Encapsulation

All attributes and methods are wrapped in the class.

By doing this attributes can only be changed through the associated class methods.

Attributes should only be visible in the scope of the class. This is referred to as “data hiding” as implementation details are hidden from the user. This is a key aspect of OOP.

main.py

class Student:
    # Private member variables (use getters/setters for access)
    __name = None
    __age = None

    def get_name(self):
        return self.__name

    def set_name(self, name):
        self.__name = name

def main():
    student1 = Student()
    student2 = Student()

    student1.set_name("Ahmed")

    print(student1.get_name())  # Using get_name to access private name

main()

Cmd Output:

Ahmed

Getters and Setters

Encapsulation refers to the process of hiding "sensitive" information from users. In order to accomplish this, certain steps need to be taken:

Private variables are restricted to access within the same class, meaning an external class has no access to it. However, it is possible to access them if we provide public getters and setters.

The get method returns the variable value, and the set method sets the value of the variable.

The syntax for both methods follows a specific pattern: they begin with either "get" or "set", followed by the variable name with the first letter capitalized.

main.py

class Student:
    __name = None
    __age = None

    def get_name(self):
        return self.__name

    def set_name(self, name):
        self.__name = name

    def get_age(self):
        return self.__age

    def set_age(self, age):
        self.__age = age

def main():
    student1 = Student()
    student2 = Student()
    student1.set_name('Alice')
    student1.set_age(12)
    student2.set_name('Bob')
    student2.set_age(13)

    print(student1.get_name())
    print(student1.get_age())
    print(student2.get_name() + ' is ' + str(student1.get_age()))

main()

Cmd Output:

Ahmed
15
Victor
16

By using concatenation you can display the data better.


Constructor

Instead of creating the object and then setting the values. You can create a constructor that creates the object and sets the values at the same time.

main.py

class Student:
  def __init__(self, name, age):
    self.name = name
    self.age = age

def main():
  student1 = Student("Alice", 20)
  student2 = Student("Bob", 22)

  print(student1.name)
  print(student2.age)

main()

Cmd Output:

Ahmed

3