Object-Oriented Programming System (OOPS) is a programming method that organizes code using objects and classes. It helps in building clean, reusable, scalable, and maintainable applications.
A class is a blueprint/template for creating objects.
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
An object is an instance of a class.
s = Student("Sai", 90)
Binding data (variables) and methods into a single unit (class). Also hides internal details.
class Bank:
def __init__(self):
self.__balance = 0 # private variable
def deposit(self, amount):
self.__balance += amount
One class acquiring properties/methods of another class.
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
print("Bark")
Same method name behaving differently based on the object.
def make_sound(animal):
animal.sound()
Hiding complex logic and showing only the necessary details. Achieved using abstract classes.
from abc import ABC, abstractmethod
class Car(ABC):
@abstractmethod
def start(self):
pass
This is how OOPS is applied in a real application:
class User:
def __init__(self, name):
self.name = name
class Order:
def __init__(self, user, amount):
self.user = user
self.amount = amount
# Polymorphism Example
class Payment:
def pay(self):
pass
class UpiPayment(Payment):
def pay(self):
print("Paid using UPI")
class CardPayment(Payment):
def pay(self):
print("Paid using Card")
# Runtime Polymorphism
payment = UpiPayment()
payment.pay()