0% found this document useful (0 votes)
2 views4 pages

Object-Oriented Programming (OOP)

The document explains the core concepts of Object-Oriented Programming (OOP) in Python, including classes and objects, encapsulation, inheritance, polymorphism, and abstraction. It illustrates how these principles enable developers to create modular, maintainable, and scalable applications by modeling real-world entities. Each concept is accompanied by code examples to demonstrate their practical application in Python.

Uploaded by

Anuj Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Object-Oriented Programming (OOP)

The document explains the core concepts of Object-Oriented Programming (OOP) in Python, including classes and objects, encapsulation, inheritance, polymorphism, and abstraction. It illustrates how these principles enable developers to create modular, maintainable, and scalable applications by modeling real-world entities. Each concept is accompanied by code examples to demonstrate their practical application in Python.

Uploaded by

Anuj Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python supports Object-Oriented Programming (OOP), a versatile paradigm that helps you

model real-world entities as objects with attributes (data) and behaviors (methods). This
approach not only makes your code modular and maintainable but also enables you to
create more scalable applications. Let's dive into the core concepts of Python OOP and
see how they can transform your projects.

1. Classes and Objects

A class is essentially a blueprint for creating objects. It lets you define structures that
encapsulate both data (attributes) and behavior (methods). Once a class is defined, you
create objects (or instances) of that class. For example, consider a simple Dog class:

class Dog:
species = "Canine" # Class attribute shared across all instances

def __init__(self, name, age):


self.name = name # Instance attribute unique to each object
self.age = age # Instance attribute

def bark(self):
return f"{self.name} says Woof!"

# Creating an object (instance) of the Dog class


buddy = Dog("Buddy", 3)
print(buddy.name) # Output: Buddy
print(buddy.species) # Output: Canine
print(buddy.bark()) # Output: Buddy says Woof!

In this example, the Dog class defines what attributes and behaviors every dog should
have, and then you create a specific dog, buddy, that carries its own state and can perform
actions like barking.

2. Encapsulation

Encapsulation involves bundling the data (attributes) and methods that operate on the
data into a single unit—your class. It also means restricting direct access to some of an
object's components, which can help prevent unintended interference and misuse of your
code. In Python, you often indicate that an attribute should be treated as private by
prefixing it with an underscore (_) or two underscores (__) for stronger name mangling. For
example:

class Computer:
def __init__(self):
self.__maxprice = 900 # Encapsulated (private) attribute

def sell(self):
return f"Selling Price: {self.__maxprice}"

def setMaxPrice(self, price):


self.__maxprice = price

comp = Computer()
print(comp.sell()) # Output: Selling Price: 900

# Attempt to change __maxprice directly — this won't affect the actual


private attribute
comp.__maxprice = 1000
print(comp.sell()) # Still outputs: Selling Price: 900

# Using the setter method to update the price


comp.setMaxPrice(1000)
print(comp.sell()) # Output: Selling Price: 1000

Encapsulation keeps the internal state of an object protected from unintended external
modifications while exposing a controlled interface for interaction.

3. Inheritance

Inheritance lets you create a new class (child or derived class) based on an existing class
(parent or base class). This enables you to reuse code and extend the functionality of
existing classes without modifying them. For example:

class Animal:
def eat(self):
return "I can eat!"
class Dog(Animal): # Inheriting from Animal
def bark(self):
return "I can bark! Woof woof!!"

dog1 = Dog()
print(dog1.eat()) # Inherited method from Animal: I can eat!
print(dog1.bark()) # Method defined in Dog: I can bark! Woof woof!!

Inheritance allows the derived class (Dog) to use methods of the base class (Animal) and
simultaneously add its own specialized functionalities. This principle is a cornerstone of
OOP that fosters reusability and logical structure in your code, as described in the Python
OOP communities ]

4. Polymorphism

Polymorphism in OOP means that different classes can be used interchangeably even if
they implement their behaviors differently. This is typically achieved through method
overriding, where a derived class provides a specific implementation of a method that is
already defined in its base class. Consider this example:

class Bird:
def sound(self):
return "Tweet tweet!"

class Parrot(Bird):
def sound(self):
return "Squawk!"

def make_sound(bird):
# Regardless of the bird type, call the sound method
print(bird.sound())

parrot = Parrot()
make_sound(parrot) # Output: Squawk!
Even though both Bird and Parrot have a method called sound, each class implements it
differently. This flexibility allows you to write more generic code that works across multiple
object types—an essential feature for designing adaptable systems ]

5. Abstraction

Abstraction is the process of hiding the complex implementation details and showing only
the necessary features of an object. Python achieves abstraction partly through abstract
base classes (via the abc module) and by designing clear, simple interfaces. Abstraction
helps reduce programming complexity and increase efficiency by allowing you to focus on
interactions at a higher level without getting bogged down by low-level details .

You might also like