Python OOPs Explaination
Python OOPs Explaination
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
```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")
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}."
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.
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
P a g e 10 | 10