Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python OOPs Explaination

chat gpt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python OOPs Explaination

chat gpt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

Python OOP (Object-Oriented Programming) Notes


INSTRUCTOR: WAJAHAT HUSSAIN

Object-oriented Programming (OOP) is a programming paradigm that structures


code around objects. Let's dive deep into its principles and how they’re
implemented in Python.
1. What is OOP?
OOP structures code around objects rather than functions. Objects represent
realworld entities and are instances of classes—the blueprints for creating objects.

Core Concepts
Classes; Blueprints for objects
Objects; Instances of classes; hold data and behavior
P a g e 1 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

Attributes; Data stored within an object


Methods; Functions defining behaviors of an object
2. Key OOP Principles in Python
Let's break down the four main pillars of OOP with Python examples.
2.1 Encapsulation
Definition: Bundles data and functions within a class and restricts access from
outside.
Purpose: Protects internal state and simplifies the interface.

In the concept of Encapsulation, a Class in Python contains:


Methods: Functions that define the behaviors of the class.
Variables: Attributes that hold the data for the class.
Code Example:

```python
class Car:
def init(self, brand, model):
self.brand = brand Public attribute
self.model = model Private attribute
P a g e 2 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

def getmodel(self):
return self.model
```

2.2 Inheritance
Definition: Allows a class (child) to inherit attributes and methods from another
class (parent).
Purpose: Enables code reuse and establishes an "isa" relationship.

P a g e 3 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

Code Example:

```python
class Vehicle:
def startengine(self):
print("Engine started")

class Car(Vehicle): Car inherits from Vehicle


pass

mycar = Car()
mycar.startengine() Inherits method from Vehicle
```

2.3 Polymorphism
Definition: Allows different classes to be treated as instances of the same class
through shared methods.
Purpose: Enables flexibility in function usage.

P a g e 4 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

Code Example:

```python
class Bird:
def sound(self):
return "Chirp"

class Dog:
def sound(self):
return "Bark"

//Polymorphic function
def makesound(animal):
print(animal.sound())

makesound(Bird()) Chirp
makesound(Dog()) Bark
P a g e 5 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

```
2.4 Abstraction
Definition: Hides complex details and shows only the essential aspects.
Purpose: Simplifies code and reduces complexity.

Code Example:

```python
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Cat(Animal):
def sound(self):
return "Meow"

P a g e 6 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

```
3. Defining Classes and Objects in Python
Class Syntax:
```python
class ClassName:
def init(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2

def method(self):
pass
```

Creating an Object:
```python
myobject = ClassName(value1, value2)
```
RealWorld Example
```python
class Person:
def init(self, name, age):

P a g e 7 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

self.name = name
self.age = age

def greet(self):
return f"Hello, my name is {self.name}."

person1 = Person("Alice", 30)


print(person1.greet()) Output: Hello, my name is Alice.
```

4. Constructors and Destructors

Constructor (`init`): Initializes an object’s state.


Destructor (`del`): Cleans up resources when an object is deleted (less commonly
used).

Code Example:

```python
class MyClass:
def init(self):
print("Object initialized")

def del(self):
P a g e 8 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

print("Object deleted")

obj = MyClass()
del obj Triggers del
```
5. Special/Magic Methods (Dunder Methods)
Python's "magic" methods start and end with double underscores (``). These
methods allow us to define custom behaviors for operators and builtin functions.

`str()`: Defines behavior for `print()`


`add()`: Defines behavior for `+`
`len()`: Defines behavior for `len()`

Example:
```python
class Book:
def init(self, title, pages):
self.title = title
self.pages = pages

def str(self):
return f"{self.title}, {self.pages} pages"

P a g e 9 | 10
OOPs In PYTHON (Object Oriented Programming) | Wajahat Hussain

book = Book("Python Guide", 300)


print(book) Output: Python Guide, 300 pages
```

6. Summary of OOP Concepts

Classes and Objects: Foundation of OOP.


Encapsulation: Protects data within objects.
Inheritance: Reuses code in a hierarchy.
Polymorphism: Flexibility through shared method names.
Abstraction: Hides complexity and reduces clutter.

Made by WAJAHAT HUSSAIN

P a g e 10 | 10

You might also like