Programmer Coding

Python OOPs Concepts

Python OOPs

Object-oriented programming (OOP) in Python is a programming paradigm that uses objects and classes in programming. Its goal is to use real-world tools such as inheritance, polytyping, and encapsulation in programming. The main idea of ​​OOP is to bind data and the functions that manage that data as a unit so that other parts of the code cannot access the data.

OOPs Concepts in Python

  • Class
  • Objects
  • Polymorphism
  • Encapsulation
  • Inheritance
  • Data Abstraction

Python Class

A Class is a set of items. a category incorporates the blueprints or the prototype from which the items are being created. it’s far a logical entity that incorporates a few attributes and strategies.

To understand the need for growing a category allows do not forget an instance, permit’s say you wanted to music the quantity of puppies which can have one of a kind attributes like breed, and age. If a listing is used, the first element may be the canine’s breed while the second detail should represent its age. allows think there are one hundred distinctive puppies, then how could  which element is meant to be which? What if you desired to feature other properties to these dogs? This lacks organization and it’s the exact need for lessons

Syntax

class ClassName:

<statement-1>

.

.

<statement-N>

Python Objects

The object is an entity that has nation and behavior. it can be any real-world integrated object like the mouse, keyboard, chair, desk, pen, and so forth.

built-in the entirety integrated Python is an object, and nearly built-in has attributes and techniques. All features have a characteristic __doc__, which returns the doc string built-in with built integrated feature source code.

Example

class Car:

def __init__(self, make, model, year):

self.make = make

self.model = model

self.year = year

def display_info(self):

print(f”Car: {self.year} {self.make} {self.model}”)

my_car = Car(“Toyota”, “Corolla”, 2020)

print(my_car.make)  # Output: Toyota

print(my_car.model)  # Output: Corolla

print(my_car.year)   # Output: 2020

my_car.display_info()

Output

Toyota

Corolla

2020

Car: 2020 Toyota Corolla

Python Polymorphism

Polymorphism contains two words “poly” and “morphs”. Poly means many, and morph means shape. By polymorphism, we understand that one task can be performed in different ways. For example – you have a class animal, and all animals speak. But they speak differently. Here, the “speak” behavior is polymorphic in a sense and depends on the animal. So, the abstract “animal” concept does not actually “speak”, but specific animals (like dogs and cats) have a concrete implementation of the action “speak”.

Example

class Animal:

def make_sound(self):

pass

class Dog(Animal):

def make_sound(self):

print(“Woof!”)

class Cat(Animal):

def make_sound(self):

print(“Meow!”)

dog = Dog()

cat = Cat()

dog.make_sound()  # Output: Woof!

cat.make_sound()

Output

Woof!

Meow!

Python Encapsulation

Encapsulation is also an essential aspect of object-oriented programming. It is used to restrict access to methods and variables. In encapsulation, code and data are wrapped together within a single unit from being modified by accident.

Example

class BankAccount:

def __init__(self):

self.balance = 0

def deposit(self, amount):

self.balance += amount

def withdraw(self, amount):

self.balance -= amount

def get_balance(self):

return self.balance

my_account = BankAccount()

my_account.deposit(100)

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

my_account.withdraw(50)

print(my_account.get_balance())

Output

100

50

Python Inheritance

Inheritance is a key part of the project’s orientation and follows the concept of heritage in the real world. Specifies that the child object inherits all properties and attributes of the parent object.

Using inheritance we can create a class that uses all the features and behaviors of another class. The new class is called a class or subclass, and the class to which attributes are assigned is called a base class or main class.

Example

class Vehicle:

def move(self):

print(“Vehicle is moving”)

class Car(Vehicle):

pass

car = Car()

car.move()

Output

Vehicle is moving

Python Data Abstraction

Data abstraction and encapsulation both are regularly used as synonyms. each are nearly synonyms because statistics abstraction is performed thru encapsulation.

Abstraction is used to hide internal information and show best functionalities. Abstracting some thing method to offer names to matters so that the call captures the core of what a feature or a whole application does.

Example

class Rectangle:

def __init__(self, width, height):

self.width = width

self.height = height

def area(self):

return self.width * self.height

rect = Rectangle(5, 10)

print(rect.area())

Output

50

Aspect Object-oriented Programming (OOP) Procedural Programming
Paradigm Emphasizes objects and classes. Focuses on procedures and functions.
Primary Unit Objects are the primary unit of code. Functions are the primary unit of code.
Data and Behavior Combines data and behavior into objects. Data and behavior are separate entities.
Encapsulation Encapsulation hides the internal state of an object and only exposes necessary functionality through methods. Data and functions are not encapsulated into a single unit.
Inheritance Supports inheritance, allowing classes to inherit attributes and methods from other classes. Does not typically support inheritance.
Polymorphism Supports polymorphism, allowing objects of different classes to be treated as objects of a common superclass. Does not typically support polymorphism.
Complexity Management Well-suited for managing complexity in large projects through modular design and encapsulation. May lead to complex code and require additional effort for managing large projects.
Example Languages Python, Java, C++, etc. C, Pascal, Basic, etc.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top