Python OOPs Concepts

In this tutorial, you will learn about OOPs Concepts In Python

Understanding Object-Oriented Programming


Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects.

An object is a self-contained entity that consists of both data and methods that operate on that data.

Python is a versatile programming language that supports OOP principles, making it easier for developers to design, organize, and maintain their code.

 

Key Concepts of OOP


Classes and Objects

Class: A class is a blueprint for creating objects. It defines the attributes (data) and methods (functions) that the objects of the class will have.

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
Object: An object is an instance of a class. It is a concrete realization of the class, with specific values for its attributes.
my_dog = Dog(name="Buddy", age=3)

Encapsulation

Encapsulation is the bundling of data and methods that operate on that data within a single unit, i.e., a class. This helps in hiding the internal details of the object and only exposing what is necessary.

Inheritance

Inheritance allows a class (subclass or derived class) to inherit the properties and methods of another class (base class or parent class). This promotes code reuse and establishes a relationship between classes.

class GermanShepherd(Dog):
    def __init__(self, name, age, color):
        super().__init__(name, age)
        self.color = color


Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables a single interface to represent different types.

def print_pet_details(pet):
    print(f"Name: {pet.name}, Age: {pet.age}")

my_pet = Dog(name="Fido", age=2)
print_pet_details(my_pet)

my_gsd = GermanShepherd(name="Rex", age=4, color="Black and Tan")
print_pet_details(my_gsd)

 

Declaring Classes and Creating Objects


To declare a class, use the class keyword. The __init__ method is a special method called a constructor, used for initializing object attributes.

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

my_car = Car(make="Toyota", model="Camry", year=2022)

 

Accessing Attributes and Methods


Access class attributes and methods using the dot notation.
print(my_car.make)  # Output: Toyota

class Car:
    # ... (previous code)
    
    def display_info(self):
        print(f"{self.year} {self.make} {self.model}")

my_car.display_info()  # Output: 2022 Toyota Camry

 

Encapsulation and Access Modifiers


In Python, encapsulation is achieved using private and protected attributes. Conventionally, a single underscore (_) is used to indicate a protected attribute, and a double underscore (__) for a private attribute.

```python
class BankAccount:
    def __init__(self, balance):
        self._balance = balance  # Protected attribute
        self.__account_number = "123456"  # Private attribute

 

Inheritance


Inheritance is declared by including the base class in parentheses after the derived class name. The super() function is used to call the constructor of the parent class.

class ElectricCar(Car):
    def __init__(self, make, model, year, battery_capacity):
        super().__init__(make, model, year)
        self.battery_capacity = battery_capacity

 

Polymorphism


Polymorphism allows for flexibility in function parameters, enabling the use of objects of different classes.

def display_vehicle_info(vehicle):
    vehicle.display_info()

electric_car = ElectricCar(make="Tesla", model="Model 3", year=2023, battery_capacity="75 kWh")
display_vehicle_info(my_car)       # Output: 2022 Toyota Camry
display_vehicle_info(electric_car)  # Output: 2023 Tesla Model 3

 

Conclusion


Understanding the basics of OOP in Python is crucial for writing modular and maintainable code. This chapter provided an overview of key concepts, including classes, objects, encapsulation, inheritance, and polymorphism. Subsequent chapters will delve deeper into each concept, providing practical examples and use cases.
 

© 2022-2023 All rights reserved.