The concept of OOPS in Python

 The concept of OOPS in Python

Object-Oriented Programming (OOP) is a programming paradigm that organizes data and behavior into reusable and self-contained objects. Python is a multi-paradigm programming language, which means it supports different programming styles including OOP. Here are the key concepts of OOP in Python:


### 1. Classes and Objects:


- Class: A class is a blueprint for creating objects. It defines a set of attributes and methods that will characterize any object created from the class.

  

  ```python

  class Car:

      def __init__(self, brand, model):

          self.brand = brand

          self.model = model


      def display_info(self):

          print(f"{self.brand} {self.model}")

  ```


- Object: An object is an instance of a class. It is a concrete entity based on a class and created from the class blueprint.


  ```python

  car1 = Car("Toyota", "Corolla")

  car1.display_info()  # Output: Toyota Corolla

  ```


### 2. Attributes and Methods:


- Attributes: Attributes are data members that represent the state of the object. They are like variables that belong to the class or instance.


  ```python

  class Circle:

      def __init__(self, radius):

          self.radius = radius


      def area(self):

          return 3.14 * self.radius * self.radius

  ```


- Methods: Methods are functions defined inside a class. They represent the behavior of the object and can access and modify the object's attributes.


  ```python

  circle1 = Circle(5)

  print(circle1.area())  # Output: 78.5

  ```


### 3. Encapsulation:


Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit known as a class. It hides the internal state of the object from the outside world and only allows access through public methods (getters and setters).


```python

class BankAccount:

    def __init__(self, balance=0):

        self.__balance = balance  # Private attribute


    def get_balance(self):

        return self.__balance  # Getter method


    def deposit(self, amount):

        self.__balance += amount


account = BankAccount()

account.deposit(100)

print(account.get_balance())  # Output: 100

```


### 4. Inheritance:


Inheritance allows a class (subclass/derived class) to inherit the properties and methods of another class (superclass/base class). It promotes code reusability.


```python

class Animal:

    def sound(self):

        pass


class Dog(Animal):

    def sound(self):

        print("Woof!")


dog = Dog()

dog.sound()  # Output: Woof!

```


### 5. Polymorphism:


Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables flexibility and interchangeability in object usage.


```python

class Cat(Animal):

    def sound(self):

        print("Meow!")


animals = [Dog(), Cat()]

for animal in animals:

    animal.sound()  # Output: Woof! Meow!

```


These are the fundamental concepts of Object-Oriented Programming in Python. Understanding and applying these concepts can lead to more organized, efficient, and maintainable code structures.


Comments

Popular posts from this blog

Programming in Python