Cross_Column

Monday, 19 January 2026

Object Oriented Programming (OOP) in Python




Object Oriented Programming (OOP) in Python

Object Oriented Programming (OOP) is a programming approach that organizes code into objects and classes. Python fully supports OOP, making programs more modular, reusable, scalable, and easy to maintain.


Why Use OOP in Python?

  • Better code organization
  • Reusability of code
  • Easy maintenance and updates
  • Real-world problem modeling
  • Widely used in automation frameworks and applications

Basic OOP Concepts in Python

Python OOP is based on four main principles:

  1. Class
  2. Object
  3. Encapsulation
  4. Inheritance
  5. Polymorphism
  6. Abstraction

1. Class in Python

A class is a blueprint or template used to create objects. It defines variables (attributes) and functions (methods).


class Car:
    brand = "Toyota"

    def start(self):
        print("Car started")

Here:

  • Car is a class
  • brand is a class variable
  • start() is a method


2. Object in Python

An object is an instance of a class. It represents a real-world entity created using a class.


my_car = Car()
my_car.start()
print(my_car.brand)

Output:


Car started
Toyota


3. The __init__() Constructor

The __init__() method is a special constructor that runs automatically when an object is created.


class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def show_details(self):
        print(self.name, self.salary)


emp1 = Employee("Chandan", 50000)
emp1.show_details()


4. Encapsulation

Encapsulation means hiding internal data and allowing access through methods only.


class Account:
    def __init__(self, balance):
        self.__balance = balance   # private variable

    def get_balance(self):
        return self.__balance

Note: Variables starting with __ are private.


5. Inheritance

Inheritance allows a class to reuse properties and methods of another class.


class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def bark(self):
        print("Dog barks")


dog = Dog()
dog.speak()
dog.bark()


6. Polymorphism

Polymorphism means same method name but different behavior.


class Bird:
    def fly(self):
        print("Bird can fly")

class Penguin(Bird):
    def fly(self):
        print("Penguin cannot fly")


b = Bird()
p = Penguin()

b.fly()
p.fly()


7. Abstraction

Abstraction hides implementation details and shows only essential features. Python uses abstract classes for abstraction.


from abc import ABC, abstractmethod

class Vehicle(ABC):

    @abstractmethod
    def start(self):
        pass


Real-World Example (Automation Testing)


class Browser:
    def open(self):
        print("Opening browser")

class Chrome(Browser):
    def open(self):
        print("Opening Chrome Browser")

This approach is widely used in Selenium and Robot Framework projects.


Advantages of OOP in Python

  • Code reusability
  • Easy debugging
  • Improved security
  • Better scalability

OOP Interview Questions

  • What is OOP?
  • Difference between class and object
  • Explain encapsulation with example
  • What is method overriding?
  • Explain inheritance in Python

Conclusion

Object Oriented Programming in Python helps developers write clean, reusable, and scalable code. Understanding OOP is mandatory for Python developers, automation testers, and framework designers.

👉 Continue learning Python with practical examples on https://www.way2testing.com

No comments:

Post a Comment

Few More

Multithreading and Multiprocessing in Python

Multithreading and Multiprocessing in Python Python provides powerful tools to perform multiple tasks simultaneously usi...