📘 PAPER 2: OBJECT ORIENTED PROGRAMMING WITH PYTHON UNIT 3 – Classes, Objects & OOP Concepts (university of allahabad)

 

🔴 UNIT 3 – Classes, Objects & OOP Concepts


1️⃣ Class and Object


✅ Class

A class is a blueprint or template used to create objects.

📌 It defines:

  • Data members (variables)

  • Member functions (methods)

Example:

class Student: def show(self): print("This is a student")

✅ Object

An object is an instance of a class.

s1 = Student() s1.show()

🔹 Key Difference

ClassObject
BlueprintReal-world entity
LogicalPhysical
No memoryUses memory

2️⃣ Abstract Data Type (ADT)

✅ Definition

An ADT defines:

  • What operations are to be performed

  • Not how they are implemented

It focuses on behavior, not implementation.

Example:


3️⃣ Classes and Objects in Python

Example Program:

class Person: def __init__(self, name): self.name = name def display(self): print("Name:", self.name) p = Person("Rahul") p.display()

4️⃣ Constructor (__init__)

Definition:

A constructor is a special method used to initialize objects.

Example:

class Student: def __init__(self, roll): self.roll = roll

5️⃣ Features of OOP in Python


🔹 1. Encapsulation

Binding data and methods together.

class Bank: def __init__(self): self.__balance = 5000

Data hiding
✔ Security


🔹 2. Abstraction

Showing essential features and hiding internal details.

Achieved using:


🔹 3. Inheritance

Child class inherits properties of parent class.

class Parent: def show(self): print("Parent") class Child(Parent): pass

Types of Inheritance:

  1. Single

  2. Multiple

  3. Multilevel

  4. Hierarchical

  5. Hybrid


🔹 4. Polymorphism

Same function behaves differently.

Example:

print(len("Python")) print(len([1,2,3]))

6️⃣ Abstract Class

Definition:

An abstract class contains abstract methods (methods without body).

Using abc module:

from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass

7️⃣ Scope in Python

Types of Scope:

  1. Local

  2. Global

  3. Non-local

  4. Built-in


Example:

x = 10 # global def fun(): x = 5 # local print(x)

8️⃣ Multithreading in Python


✅ Definition

Multithreading means executing multiple threads simultaneously.


Is Multithreading Supported in Python?

✔ Yes
❌ But limited due to GIL (Global Interpreter Lock)


Example:

import threading def display(): print("Hello") t = threading.Thread(target=display) t.start()

Advantages:


Disadvantages:

  • Complex debugging

  • GIL limits performance

Comments

Popular Posts