Python OOP

Object-Oriented Programming (OOP) is a way of writing code by thinking about real-world objects. Each object has data (like color, size) and actions (like move, stop). This makes programs easier to understand and manage.

The main goal of OOP is to make code reusable, organized, and scalable. By using this system, developers can create a “blueprint” once and use it thousands of times without rewriting the same logic. It helps manage complexity by breaking down a giant project into small, manageable pieces that can be tested and updated independently.

Key Features of OOP

Python supports OOP with features like classes, objects, inheritance, polymorphism, encapsulation, and abstraction. These features help us reuse code, hide details, and make programs flexible and organized.

Four Pillars of OOP:

The main principles of OOP are Inheritance, Polymorphism, Encapsulation, and Abstraction. They allow us to reuse logic, change behavior, protect data, and hide complexity. These are the building blocks of OOP.

Classes (The Blueprint)

A Class is the blueprint or template used to create objects. It defines the structure—what data it will hold and what actions it can take—but it doesn’t contain the actual data itself yet. Think of a class as a blank form; it tells you what information is needed (Name, Age), but it is not a specific person until filled out.
class Pen:
  def write(self):
      print("Writing notes")

Objects in Python

An object is the actual thing created from a class. If a class is a plan, the object is the product.
p = Pen()
p.write() #output: Writing notes

Attributes

Attributes are the data stored inside an object. They describe the object’s state.
class Pen:
  def __init__(self, color):
      self.color = color

p = Pen("Black")
print(p.color)   # Output: Black

Methods

Methods are functions inside a class that define what an object can do.
class Fan:
  def start(self):
      print("Fan is running")

f = Fan()
f.start()   # Output: Fan is running

Constructor (__init__ Method)

The constructor is a special method that runs automatically when an object is created. It sets initial values.
class Student:
  def __init__(self, name):
      self.name = name

s = Student("Pankaj")
print(s.name)   # Output: Pankaj

Inheritance

Inheritance allows one class to use features of another class. It avoids repeating code.
class Vehicle:
  def move(self):
      print("Vehicle is moving")

class Bike(Vehicle):
  pass

b = Bike()
b.move()   # Output: Vehicle is moving

Polymorphism

Polymorphism means same action, different behavior depending on the object.
class Cat:
  def sound(self):
      print("Meow")

class Dog:
  def sound(self):
      print("Woof")

for pet in [Cat(), Dog()]:
  pet.sound()

Encapsulation

Encapsulation means hiding details and controlling access to data. We use private variables to protect information.
from abc import ABC, abstractmethod

class Appliance(ABC):
  @abstractmethod
  def start(self):
      pass

class WashingMachine(Appliance):
  def start(self):
      print("Washing clothes")

wm = WashingMachine()
wm.start()   # Output: Washing clothes

Abstraction

Abstraction means showing only the important things and hiding the complex details.
from abc import ABC, abstractmethod

class Appliance(ABC):
  @abstractmethod
  def start(self):
      pass

class WashingMachine(Appliance):
  def start(self):
      print("Washing clothes")

wm = WashingMachine()
wm.start()   # Output: Washing clothes
Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top