Object-Oriented Programming (OOP) Concepts Explained
The one idea behind OOP
Programs grow messy when data and the functions that use it live far apart. Object-Oriented Programming fixes this by bundling related data and behaviour into objects. Think of a car: its data (colour, speed) and its behaviour (accelerate, brake) belong together — you never look for the brake pedal in a different building.
Classes and objects
A class is a blueprint; an object is a thing built from it. One class, many objects:
class Student:
def __init__(self, name, semester):
self.name = name
self.semester = semester
def introduce(self):
return "I am " + self.name + ", semester " + str(self.semester)
s1 = Student("Ayesha", 3)
s2 = Student("Bilal", 5)
print(s1.introduce())__init__ runs when an object is created and stores its starting data. Each object keeps its own values.
The four core concepts
- Encapsulation — keep data inside the object and let others use it through methods, not by reaching in directly
- Inheritance — a new class can extend an existing one: a
GraduateStudentis aStudentplus a thesis topic, reusing everything already written - Polymorphism — different objects answer the same method name in their own way: both a
Dogand aCatcanspeak(), differently - Abstraction — hide inner complexity behind a simple interface, like a steering wheel hiding the engine
When OOP helps — and when it does not
OOP shines in larger programs with many related entities: students in a university system, products in a shop, enemies in a game. For a ten-line script, plain functions are simpler. Professional skill means choosing the simpler tool that still keeps the code clear.
Practice task (45 minutes)
Write a Book class with title, author and pages, plus a method that returns a one-line description. Create three books, store them in a list, and print each description with a loop. Then add a EBook subclass that also stores file size — you have just used inheritance for real.
Class = blueprint, object = instance, and encapsulation, inheritance, polymorphism and abstraction are the four ideas to master. One small class you write yourself teaches more than ten definitions.