OOPS
OOP stands for Object-Oriented Programming.
It is a programming style where we organize code using objects and classes to make it:
Easier to understand
Easier to maintain
Easier to reuse
OOPS Concepts:
1. Class – Blueprint for creating objects.
2. Object – Instance of a class.
3. Constructor (__init__) – Method to initialize object data.
4. Self – Refers to the current object.
5. Method – Function defined inside a class.
6. Encapsulation, Inheritance, Polymorphism, Abstraction – OOP principles
Feature Meaning Useful
Modularity Code is divided into small parts Easier to manage
(classes)
Reusability Use the same class again and again Save time
Encapsulation Hides internal details Keeps data safe
Inheritance Child class can use parent class code Avoid code duplication
Polymorphism Same function can behave differently More flexibility
Example:
1. DOG
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
print(self.name + " says Woof!")
dog1 = Dog("Tommy")
dog1.speak() # Output: Tommy says Woof!
2.Student
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def display(self):
print(f"{self.name} is in grade {self.grade}")
s1 = Student("Ravi", 10)
s1.display()
3.Car
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def show(self):
print(f"This is a {self.color} {self.brand}")
c1 = Car("Toyota", "Red")
c1.show()
4.Calculator
class Calculator:
def __init__(self, a, b):
self.a = a
self.b = b
def add(self):
print("Sum:", self.a + self.b)
calc = Calculator(5, 3)
calc.add()
5. Book
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def details(self):
print(f"'{self.title}' by {self.author}")
b1 = Book("Wings of Fire", "A.P.J. Abdul Kalam")
b1.details()
6. Employee
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def show_salary(self):
print(f"{self.name}'s salary is ₹{self.salary}")
e1 = Employee("Anita", 50000)
e1.show_salary()