Object-Oriented Programming in Python
What is OOP?
Object-Oriented Programming (OOP) organizes code around objects — things that have data (attributes) and actions (methods). A class is the blueprint; an object is a real item made from it.
A "Student" class is the blueprint. Aman and Riya are two objects (students) made from that blueprint, each with their own name and marks.
Class and Object
class Dog:
sound = "Bark" # attribute shared by all dogs
d1 = Dog() # create an object
print(d1.sound)class Dog is the blueprint; d1 = Dog() makes one real dog object.
__init__ Constructor
__init__ runs automatically when an object is created. It sets up each object's own data. self refers to the current object.
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
s1 = Student("Aman", 95)
print(s1.name, s1.marks)Program 1: Student Class
class Student:
def __init__(self, name, roll, marks):
self.name = name
self.roll = roll
self.marks = marks
s1 = Student("Aman", 1, 88)
s2 = Student("Riya", 2, 95)
print(s1.name, "scored", s1.marks)
print(s2.name, "scored", s2.marks)One class, two independent objects — each keeps its own name, roll and marks.
Program 2: Methods (Behaviour)
A method is a function inside a class that acts on the object's data.
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def result(self):
return "PASS" if self.marks >= 33 else "FAIL"
s1 = Student("Aman", 75)
print(s1.name, ":", s1.result())Program 3: Inheritance
Inheritance lets a class reuse another class's code. The child gets everything from the parent and can add more.
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hi, I am", self.name)
class Teacher(Person): # Teacher inherits Person
def teach(self):
print(self.name, "is teaching")
t = Teacher("Mr. Rao")
t.greet() # from Person
t.teach() # from TeacherTeacher(Person) means Teacher inherits Person — it can use greet() for free.
Program 4: Bank Account (Encapsulation)
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds")
else:
self.balance -= amount
acc = BankAccount("Aman", 1000)
acc.deposit(500)
acc.withdraw(300)
print("Balance:", acc.balance)The data (balance) and the actions on it (deposit, withdraw) live together — this is encapsulation.
Four Pillars of OOP
| Pillar | Meaning |
|---|---|
| Encapsulation | Data + methods bundled in one class |
| Inheritance | Child class reuses parent code |
| Polymorphism | Same method name, different behaviour |
| Abstraction | Hide complex details, show only what is needed |
Common Mistakes
- Forgetting
selfas the first parameter of methods. - Forgetting brackets when creating an object:
StudentvsStudent(). - Mixing up the class (blueprint) with the object (real item).
Practice Tasks
- Make a
Carclass with brand and speed, and a method to show details. - Add a method to the Student class that prints the grade.
- Create a
Animalparent andDog/Catchildren that override asound()method. - Build a simple
Rectangleclass with anarea()method.
Summary
- OOP organizes code into classes (blueprints) and objects (real items).
__init__sets up each object;selfis the current object.- Methods are functions inside a class; inheritance reuses code.
- Four pillars: encapsulation, inheritance, polymorphism, abstraction.