Object-Oriented Programming in Python

📘 Python 👁 67 views 📅 Nov 05, 2025
⏱ Estimated reading time: 3 min

Object-Oriented Programming (OOP) in Python

Python supports Object-Oriented Programming (OOP), which allows you to model real-world entities using classes and objects. OOP provides concepts like Encapsulation, Inheritance, Polymorphism, and Abstraction.


1. Classes and Objects

  • Class: A blueprint for creating objects. It defines attributes (variables) and methods (functions).

  • Object: An instance of a class.

Syntax:

class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old")

Creating an object:

person1 = Person("Alice", 25) person1.greet() # Hello, my name is Alice and I am 25 years old

2. The __init__ Method (Constructor)

  • Automatically called when a new object is created.

  • Used to initialize attributes.

class Car: def __init__(self, brand, model): self.brand = brand self.model = model

3. Instance and Class Variables

  • Instance variables: Unique to each object.

  • Class variables: Shared across all instances.

class Dog: species = "Canine" # class variable def __init__(self, name): self.name = name # instance variable

4. Methods

  • Instance methods: Operate on instance variables.

  • Class methods: Operate on class variables, use @classmethod.

  • Static methods: Do not access class or instance variables, use @staticmethod.

class Example: class_var = 0 def instance_method(self): print("This is an instance method") @classmethod def class_method(cls): print("This is a class method", cls.class_var) @staticmethod def static_method(): print("This is a static method")

5. Inheritance

  • Allows a class to inherit properties and methods from another class.

  • Supports code reuse and hierarchical modeling.

class Animal: def speak(self): print("Animal speaks") class Dog(Animal): # Dog inherits from Animal def speak(self): print("Dog barks") d = Dog() d.speak() # Dog barks

6. Encapsulation

  • Restricts direct access to class attributes using private variables (_ or __).

  • Use getter and setter methods to access or modify.

class BankAccount: def __init__(self, balance): self.__balance = balance # private variable def get_balance(self): return self.__balance def deposit(self, amount): self.__balance += amount

7. Polymorphism

  • Ability of different classes to respond to the same method call.

  • Supports method overriding and operator overloading.

class Cat: def speak(self): print("Meow") class Dog: def speak(self): print("Bark") def animal_sound(animal): animal.speak() animal_sound(Cat()) # Meow animal_sound(Dog()) # Bark

8. Abstraction

  • Hides internal implementation and shows only functionality.

  • Achieved using abstract classes and abc module.

from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side * self.side s = Square(4) print(s.area()) # 16

9. Special (Magic/Dunder) Methods

  • Methods with double underscores, e.g., __str__, __len__, __add__.

  • Allow objects to behave like built-in types.

class Book: def __init__(self, title, pages): self.title = title self.pages = pages def __str__(self): return f"{self.title} ({self.pages} pages)" b = Book("Python Guide", 200) print(b) # Python Guide (200 pages)

Benefits of OOP

  • Reusability through inheritance

  • Easier maintenance

  • Encapsulation improves security

  • Models real-world entities effectively

  • Supports polymorphism and abstraction


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes