---
title: "Explain the Python OOP"  
description: "Explain the Python OOP"  
author: "ICSM Computer"  
published: 2025-09-30  
updated: 2025-09-30  
canonical: https://www.mindstick.com/interview/34383/explain-the-python-oop  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 4 minutes  

---

# Explain the Python OOP

> OOP (Object-Oriented Programming) is a programming paradigm where you organize code into **classes** and **objects**.

- **Class** → A blueprint (like a template) for creating objects.
- **Object** → An instance of a class (a real thing created from the blueprint).

Example in real life:

- **Class** = `Car` (blueprint: wheels, engine, color, methods like drive, stop).
- **Object** = `my_car`, `your_car` (specific cars created from that blueprint).

## 2. Python Class & Object

```python
# Define a class
class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

    def drive(self):
        print(f"{self.color} {self.brand} is driving")

# Create objects (instances)
car1 = Car("Tesla", "Red")
car2 = Car("BMW", "Black")

car1.drive()  # Red Tesla is driving
car2.drive()  # Black BMW is driving
```

- `__init__` Constructor (runs when you create an object).
- `self` Refers to the current object instance.

## 3. OOP Principles in Python

### a) Encapsulation

Bundling data (attributes) and methods (functions) together.

```python
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # private attribute (note: name mangling)

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())  # 1500
```

### b) Inheritance

One class can inherit attributes/methods from another.

```python
class Animal:
    def speak(self):
        print("This animal makes a sound")

class Dog(Animal):
    def speak(self):
        print("Woof!")

dog = Dog()
dog.speak()  # Woof!
```

### c) Polymorphism

Same method name, different behavior depending on the object.

```python
class Cat:
    def speak(self):
        print("Meow!")

animals = [Dog(), Cat()]
for a in animals:
    a.speak()  # Woof! / Meow!
```

### d) Abstraction

Hiding details and exposing only essential features (using `abc` module).

```python
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, r):
        self.r = r
    def area(self):
        return 3.14 * self.r * self.r

c = Circle(5)
print(c.area())  # 78.5
```

## 4. Key Features in Python OOP

- **Everything is an object** → functions, numbers, strings, classes.
- **Dynamic typing** → attributes can be added to objects at runtime.
- **Multiple inheritance** → A class can inherit from more than one class.

**In short**:

- Use **classes** to create reusable blueprints.
- Create **objects** from them.
- Apply OOP principles: **Encapsulation, Inheritance, Polymorphism, Abstraction**.

## Answers

### Answer by ICSM Computer

> OOP (Object-Oriented Programming) is a programming paradigm where you organize code into **classes** and **objects**.

- **Class** → A blueprint (like a template) for creating objects.
- **Object** → An instance of a class (a real thing created from the blueprint).

Example in real life:

- **Class** = `Car` (blueprint: wheels, engine, color, methods like drive, stop).
- **Object** = `my_car`, `your_car` (specific cars created from that blueprint).

## 2. Python Class & Object

```python
# Define a class
class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

    def drive(self):
        print(f"{self.color} {self.brand} is driving")

# Create objects (instances)
car1 = Car("Tesla", "Red")
car2 = Car("BMW", "Black")

car1.drive()  # Red Tesla is driving
car2.drive()  # Black BMW is driving
```

- `__init__` Constructor (runs when you create an object).
- `self` Refers to the current object instance.

## 3. OOP Principles in Python

### a) Encapsulation

Bundling data (attributes) and methods (functions) together.

```python
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # private attribute (note: name mangling)

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())  # 1500
```

### b) Inheritance

One class can inherit attributes/methods from another.

```python
class Animal:
    def speak(self):
        print("This animal makes a sound")

class Dog(Animal):
    def speak(self):
        print("Woof!")

dog = Dog()
dog.speak()  # Woof!
```

### c) Polymorphism

Same method name, different behavior depending on the object.

```python
class Cat:
    def speak(self):
        print("Meow!")

animals = [Dog(), Cat()]
for a in animals:
    a.speak()  # Woof! / Meow!
```

### d) Abstraction

Hiding details and exposing only essential features (using `abc` module).

```python
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, r):
        self.r = r
    def area(self):
        return 3.14 * self.r * self.r

c = Circle(5)
print(c.area())  # 78.5
```

## 4. Key Features in Python OOP

- **Everything is an object** → functions, numbers, strings, classes.
- **Dynamic typing** → attributes can be added to objects at runtime.
- **Multiple inheritance** → A class can inherit from more than one class.

**In short**:

- Use **classes** to create reusable blueprints.
- Create **objects** from them.
- Apply OOP principles: **Encapsulation, Inheritance, Polymorphism, Abstraction**.


---

Original Source: https://www.mindstick.com/interview/34383/explain-the-python-oop

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
