0% found this document useful (0 votes)
16 views6 pages

Expt 6 Theory

wheowpwkwnsbdhdpslwmabzbxjsospapamsbdhspwskbdiewpwlsmsnnndnenwnwnwwj

Uploaded by

asnanmullani14
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)
16 views6 pages

Expt 6 Theory

wheowpwkwnsbdhdpslwmabzbxjsospapamsbdhspwskbdiewpwlsmsnnndnenwnwnwwj

Uploaded by

asnanmullani14
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/ 6

Name: Asnan Mullani

Roll no.:55
Div and Class: TY CSE B
Experiment Title: OOP in python

Experiment 6: To implement Program based on


Object Oriented concepts in python
### Classes, Objects, Methods, and Destructors in Python

#### 1. **Classes**
- A class is a blueprint for creating objects. It defines a set of attributes
and methods that the objects created from the class will have.
- A class is defined using the `class` keyword followed by the class name
and a colon.
- Inside the class, you can define attributes (variables) and methods
(functions).

```python
class Car:
# Class attributes
wheels = 4

# Constructor method
def __init__(self, make, model):
# Instance attributes
self.make = make
self.model = model

# Method
def display_info(self):
print(f"Car make: {self.make}, model: {self.model}")
```

#### 2. **Objects**
- An object is an instance of a class. When a class is defined, no memory is
allocated until an object of that class is created.
- You can create an object by calling the class name with its required
arguments.
```python
# Creating an object of the Car class
my_car = Car("Toyota", "Corolla")

# Accessing object attributes and methods


print(my_car.wheels) # Outputs: 4
my_car.display_info() # Outputs: Car make: Toyota, model: Corolla
```

#### 3. **Methods**
- Methods are functions defined inside a class that describe the behaviors
of the objects created from the class.
- The first argument of any method in a class must be `self`, which refers
to the instance of the object calling the method. `self` allows you to
access the attributes and methods of the class within the method.

```python
class Calculator:
def add(self, a, b):
return a + b

def subtract(self, a, b):


return a - b

# Creating an object of the Calculator class


calc = Calculator()

# Calling methods
print(calc.add(5, 3)) # Outputs: 8
print(calc.subtract(5, 3)) # Outputs: 2
```

#### 4. **Destructor**
- A destructor is a special method called when an object is about to be
destroyed. In Python, the destructor is defined by the `__del__` method.
- The destructor method is called when an object is deleted, or when the
reference count of the object reaches zero.
class MyClass:
def __init__(self, name):
self.name = name
print(f"{self.name} is created")

def __del__(self):
print(f"{self.name} is destroyed")

# Creating an object
obj = MyClass("Object1")

# Deleting the object


del obj # Outputs: Object1 is destroyed
- Destructors are less commonly used in Python because Python has an
automatic garbage collection system.

```python
class MyClass:
def __init__(self, name):
self.name = name
print(f"{self.name} is created")

def __del__(self):
print(f"{self.name} is destroyed")

# Creating an object
obj = MyClass("Object1")

# Deleting the object


del obj # Outputs: Object1 is destroyed
```

### Inheritance and Polymorphism in Python

#### 1. **Inheritance**
- Inheritance is a feature of object-oriented programming where a new
class (child or derived class) is created from an existing class (parent or
base class).
- The child class inherits all the attributes and methods of the parent class
but can also have additional attributes or methods or override existing
ones.
- The main advantage of inheritance is code reuse and the creation of
hierarchical class structures.

##### Example of Single Inheritance


```python
# Parent class
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
print(f"{self.name} makes a sound")

# Child class (inherits from Animal)


class Dog(Animal):
def speak(self):
print(f"{self.name} barks")

# Creating objects of the child class


dog = Dog("Buddy")
dog.speak() # Outputs: Buddy barks
```

##### Types of Inheritance in Python


- **Single Inheritance**: A child class inherits from one parent class.
- **Multiple Inheritance**: A child class inherits from more than one
parent class.
- **Multilevel Inheritance**: A child class inherits from a parent class,
which in turn inherits from another parent class.
- **Hierarchical Inheritance**: Multiple child classes inherit from a single
parent class.
- **Hybrid Inheritance**: A combination of two or more types of
inheritance.
```
#### 2. **Polymorphism**
- Polymorphism means "many forms," and it refers to the ability of
different classes to be treated as instances of the same class through
inheritance.
- In Python, polymorphism allows different classes to define methods with
the same name, and the correct method is called based on the object
type.
- Polymorphism is achieved through method overriding and method
overloading (although Python doesn't support true method overloading,
you can achieve it by using default arguments or variable-length
arguments).

##### Example of Polymorphism with Method Overriding


```python
class Bird:
def sound(self):
print("Birds make sounds")

class Parrot(Bird):
def sound(self):
print("Parrot says 'Squawk'")

class Sparrow(Bird):
def sound(self):
print("Sparrow says 'Chirp'")

# Polymorphism: The same method `sound` behaves differently based on


the object type
def make_sound(bird):
bird.sound()

parrot = Parrot()
sparrow = Sparrow()

make_sound(parrot) # Outputs: Parrot says 'Squawk'


make_sound(sparrow) # Outputs: Sparrow says 'Chirp'
```

You might also like