Chapter 9: Object-Oriented Programming (OOP)
Learn classes, objects, inheritance, abstraction and encapsulation using beginner-friendly Python examples.
Blueprint
Instance
Reuse
Protect
9.1 Chapter Overview
Object-Oriented Programming, commonly known as OOP, is a programming approach that organizes code using objects. An object represents a real-world item such as a student, course, employee, vehicle, product, account, or training centre.
In this chapter, learners will understand how Python uses classes and objects to create structured, reusable, and professional programs. Learners will also study four major OOP concepts: class, object, inheritance, encapsulation, and abstraction.
9.2 Chapter Objectives
- Understand the meaning and purpose of Object-Oriented Programming.
- Explain the difference between a class and an object.
- Create Python classes using attributes and methods.
- Create objects from classes.
- Understand and apply inheritance.
- Understand encapsulation and data protection.
- Understand abstraction and simplified design.
- Develop beginner-friendly OOP applications in Python.
Learning Outcomes
- Create simple Python classes and objects.
- Use constructors with __init__().
- Define object attributes and methods.
- Apply inheritance to reuse class features.
- Use encapsulation to protect data.
- Explain abstraction in simple programming terms.
9.3 What is Object-Oriented Programming?
Object-Oriented Programming is a method of programming where software is designed using objects. Each object contains data and actions. The data is stored as attributes, and the actions are written as methods.
Object
A real-world entity created from a class. Example: one student record.
Attribute
Data stored inside an object. Example: student name, age, course.
Method
A function inside a class. Example: display student details.
Class
A blueprint used to create objects. Example: Student class.
9.4 Why OOP is Important
OOP is widely used in professional software development because it makes programs more organized, reusable, scalable, and easier to maintain.
| Benefit | Explanation |
|---|---|
| Code Reusability | Classes can be reused to create many objects. |
| Better Organization | Related data and functions are grouped together. |
| Easier Maintenance | Changes can be made in one class and reused elsewhere. |
| Real-World Modelling | Programs can represent real objects such as students, courses, products and accounts. |
9.5 Classes in Python
A class is a blueprint or template used to create objects. A class defines what data an object will store and what actions the object can perform.
Basic Class Example
class Student:
name = "Amin"
course = "Python Programming"
print(Student.name)
print(Student.course)
Amin
Python Programming
9.6 Objects in Python
An object is an instance of a class. Once a class is created, we can create many objects from it.
Creating an Object
class Student:
name = "Amin"
course = "Python Programming"
student1 = Student()
print(student1.name)
print(student1.course)
Amin
Python Programming
In this example, student1 is an object created from the Student class.
9.7 Constructor using __init__()
The __init__() method is called automatically when an object is created. It is commonly used to set initial values for object attributes.
Example
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
student1 = Student("Amin", "Python")
student2 = Student("Mei Ling", "AI")
print(student1.name, student1.course)
print(student2.name, student2.course)
Amin Python
Mei Ling AI
9.8 Methods in Classes
A method is a function inside a class. Methods describe the actions an object can perform.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def display_profile(self):
print("Student Name:", self.name)
print("Course:", self.course)
student1 = Student("Amin", "Certified Python Programmer")
student1.display_profile()
Student Name: Amin
Course: Certified Python Programmer
9.9 Encapsulation
Encapsulation means keeping data and methods together inside a class. It also helps protect data from being changed directly from outside the class.
In Python, attributes can be treated as protected or private by using underscores.
| Style | Meaning | Example |
|---|---|---|
| public | Can be accessed normally. | self.name |
| protected | Suggested for internal use. | self._marks |
| private | More restricted access. | self.__password |
Encapsulation Example
class Student:
def __init__(self, name, marks):
self.name = name
self.__marks = marks
def display_marks(self):
print("Marks:", self.__marks)
student1 = Student("Amin", 85)
student1.display_marks()
Marks: 85
9.10 Getter and Setter Methods
Getter and setter methods are used to access and update private data safely.
class Student:
def __init__(self, name, marks):
self.name = name
self.__marks = marks
def get_marks(self):
return self.__marks
def set_marks(self, marks):
if marks >= 0 and marks <= 100:
self.__marks = marks
else:
print("Invalid marks")
student1 = Student("Amin", 80)
print(student1.get_marks())
student1.set_marks(90)
print(student1.get_marks())
80
90
9.11 Inheritance
Inheritance allows one class to reuse the attributes and methods of another class. The original class is called the parent class, and the new class is called the child class.
Example
class Person:
def __init__(self, name):
self.name = name
def display_name(self):
print("Name:", self.name)
class Student(Person):
def display_role(self):
print("Role: Student")
student1 = Student("Amin")
student1.display_name()
student1.display_role()
Name: Amin
Role: Student
9.12 Types of Inheritance
| Type | Description | Example Idea |
|---|---|---|
| Single Inheritance | One child class inherits from one parent class. | Student inherits from Person. |
| Multilevel Inheritance | A class inherits from another child class. | GraduateStudent inherits from Student. |
| Multiple Inheritance | One child class inherits from more than one parent class. | TeachingAssistant inherits from Student and Employee. |
9.13 Abstraction
Abstraction means showing only important details and hiding unnecessary implementation details. In simple terms, users can use a feature without knowing how it works internally.
For example, when a student clicks a login button, they do not need to know the full coding logic behind authentication. They only see the simple action: enter username and password.
Simple Abstraction Example
class Payment:
def process_payment(self):
print("Processing payment securely...")
payment1 = Payment()
payment1.process_payment()
Processing payment securely...
9.14 Practical Example: Course Registration System
This example uses OOP to create a simple student registration system.
class Student:
def __init__(self, name, course, fee):
self.name = name
self.course = course
self.fee = fee
def display_registration(self):
print("----- Registration Details -----")
print("Student Name:", self.name)
print("Course:", self.course)
print("Course Fee: RM", self.fee)
student1 = Student("Amin", "Certified Python Programmer", 1500)
student1.display_registration()
----- Registration Details -----
Student Name: Amin
Course: Certified Python Programmer
Course Fee: RM 1500
9.15 Practical Example: Employee and Trainer Inheritance
class Employee:
def __init__(self, name):
self.name = name
def display_employee(self):
print("Employee Name:", self.name)
class Trainer(Employee):
def display_trainer_role(self):
print("Role: Python Trainer")
trainer1 = Trainer("Dr. Kumar")
trainer1.display_employee()
trainer1.display_trainer_role()
Employee Name: Dr. Kumar
Role: Python Trainer
9.16 Common Beginner Mistakes
| Mistake | Problem | Correction |
|---|---|---|
| Forgetting self | Methods cannot access object attributes properly. | Always include self as the first method parameter. |
| Wrong indentation | Methods may not belong to the class. | Indent methods correctly inside the class. |
| Not using __init__ correctly | Object attributes may not initialize. | Use __init__(self, parameters) properly. |
| Confusing class and object | Students may think they are the same. | Class is a blueprint; object is created from the blueprint. |
9.17 Hands-On Practice
Activity 1: Create a Student Class
Create a class named Student with name, age and course attributes. Create one object and display the details.
Activity 2: Add a Method
Add a method named display_profile() to print student details clearly.
Activity 3: Encapsulation
Create a private marks attribute and use getter and setter methods to access and update marks.
Activity 4: Inheritance
Create a parent class Person and a child class Student. Reuse the name attribute from the parent class.
Mini Project: Student Management OOP System
Create an OOP-based student management program. The program must include a Student class, constructor, attributes, display method, private marks, getter and setter, and an inherited class named CertifiedStudent.
9.18 Final Assessment Quiz
Answer the following questions. Correct Answer = +1 Mark Wrong Answer = -0.5 Mark
1. Object-Oriented Programming organizes code using classes and objects.
2. A class is a blueprint used to create objects.
3. An object is an instance of a class.
4. The __init__() method is called automatically when an object is created.
5. The self keyword refers to the current object.
6. Inheritance allows one class to reuse features from another class.
7. Encapsulation helps protect data inside a class.
8. Abstraction means showing only important details and hiding unnecessary details.
9. A method is a function inside a class.
10. In OOP, classes and objects are exactly the same thing.
Your Score: 0
Final Practical Assessment
Develop a Python program named oop_student_management.py. The program must include a Student class, constructor, attributes, display method, encapsulated marks, getter and setter methods, and a CertifiedStudent class that inherits from Student.
9.19 Chapter Summary
In this chapter, learners studied Object-Oriented Programming in Python. They learned how to create classes, objects, constructors, attributes and methods. They also explored inheritance, encapsulation and abstraction, which are important principles in professional software development.