B3.1.1 Evaluate the fundamentals of OOP.
• Model real-world entities using OOP concepts: classes, objects, inheritance, encapsulation, polymorphism
• The advantages and disadvantages of using OOP in various programming scenarios
The big idea
Object-oriented programming (OOP) is a way of writing programs that models things in the real world as objects. An object is something that has data (what it knows) and methods (what it can do). These objects are created from classes, which are like blueprints. OOP makes it easier to organize large programs, reuse code, and build systems that can grow over time.
The core principles—encapsulation, inheritance, and polymorphism—give us tools to manage complexity, keep code organized, and extend functionality without rewriting everything from scratch. But OOP also has some drawbacks, such as extra complexity in simple programs and possible performance costs.
Core OOP concepts
- Class – A template or blueprint that defines what data and methods its objects will have.
- Object – A specific thing made from a class; it has its own copy of the data described in the class.
- Encapsulation – Keeping the details of how an object works hidden, while allowing access through controlled methods.
- Inheritance – Letting one class get the data and methods from another, so we don’t repeat code.
- Polymorphism – The ability for different classes to respond to the same method in their own way.
Simple example
Let’s model a school:
# A class is a blueprint
class Person:
def __init__(self, name):
self.name = name # data (attribute)
def introduce(self):
return f"Hello, my name is {self.name}."
# Inheritance: Student is a Person
class Student(Person):
def __init__(self, name, grade):
super().__init__(name) # reuse Person's code
self.grade = grade
# Polymorphism: Student has its own version of introduce()
def introduce(self):
return f"Hi, I'm {self.name} and I'm in grade {self.grade}."
# Inheritance: Teacher is a Person
class Teacher(Person):
def __init__(self, name, subject):
super().__init__(name)
self.subject = subject
def introduce(self):
return f"Hello, I'm {self.name} and I teach {self.subject}."
# Using the classes
people = [
Student("Alex", 10),
Teacher("Mr. Smith", "Math"),
Person("Jordan")
]
for p in people:
print(p.introduce()) # Polymorphic call
How the example shows OOP:
- Classes/Objects:
Person,Student, andTeacherare classes;AlexandMr. Smithare objects. - Inheritance:
StudentandTeacherreuse code fromPerson. - Polymorphism: The same method name
introduce()produces different results depending on the object type. - Encapsulation: The
name,grade, andsubjectattributes are part of each object and accessed in controlled ways.
Advantages of OOP
- Easier to understand complex systems – Models real-world entities clearly.
- Code reuse – Shared code in a base class avoids repetition.
- Extensibility – New object types can be added without changing existing code.
- Encapsulation – Internal details can change without affecting other parts of the program.
Disadvantages of OOP
- Overhead – More code is needed to set up classes for small programs.
- Learning curve – Concepts like inheritance and polymorphism can be difficult for beginners.
- Performance costs – Extra steps like method lookups can be slower than procedural code in performance-critical situations.
When OOP is most useful
- Programs with many different kinds of things that share features (e.g., school systems, video games, banking systems).
- Projects that will grow and change over time.
- Systems built by teams, where clear structure helps different people work on different parts.