
Object-Oriented Programming (OOP) is a method of designing programs using objects and classes. Each object represents a real-world entity with specific properties and behaviors. Classes act as templates for creating multiple objects with similar characteristics. OOP emphasizes encapsulation, inheritance, and polymorphism to manage data and behavior efficiently. This approach helps in modeling real-world problems in programming.
Benefits of OOP — Modularity, Reusability, and Easier Maintenance: OOP allows code to be divided into modules, making it easier to manage. Classes and objects can be reused in multiple programs or parts of a program, reducing repetition. Programs built with OOP are easier to maintain and update because changes in one part do not affect the entire system. The structure of OOP supports scalability and adaptability in software development. It also improves collaboration by making code more understandable and organized.
Core concepts of OOP: classes and objects, encapsulation, inheritance, and polymorphism. These concepts make programs organized, reusable, and easier to manage.
آبجیکٹ اورینٹڈ پروگرامنگ (OOP) آبجیکٹس اور کلاسز سے پروگرام ڈیزائن کرنے کا طریقہ ہے۔ ہر آبجیکٹ مخصوص خصوصیات اور رویوں کے ساتھ حقیقی دنیا کی شے کی نمائندگی کرتا ہے۔ کلاسز ایک جیسی خصوصیات والے کئی آبجیکٹس بنانے کے لیے ٹیمپلیٹس کا کام کرتی ہیں۔ OOP ڈیٹا اور رویے کو مؤثر طریقے سے منظم کرنے کے لیے انکیپسولیشن، انہیریٹنس اور پولیمورفزم پر زور دیتا ہے۔ یہ طریقہ پروگرامنگ میں حقیقی دنیا کے مسائل کا ماڈل بنانے میں مدد دیتا ہے۔
OOP کے فوائد — ماڈیولرٹی، دوبارہ استعمال، آسان دیکھ بھال: OOP کوڈ کو ماڈیولز میں تقسیم کرنے کی اجازت دیتا ہے، جس سے انتظام آسان ہو جاتا ہے۔ کلاسز اور آبجیکٹس کئی پروگرامز یا پروگرام کے حصوں میں دوبارہ استعمال ہو سکتے ہیں، جس سے دہراؤ کم ہوتا ہے۔ OOP سے بنے پروگرامز برقرار رکھنا اور اپ ڈیٹ کرنا آسان ہوتے ہیں کیونکہ ایک حصے میں تبدیلی پورے سسٹم پر اثر نہیں ڈالتی۔ OOP کی ساخت سافٹ ویئر ڈویلپمنٹ میں اسکیل ایبلٹی اور موافقت کو سپورٹ کرتی ہے۔ یہ کوڈ کو زیادہ قابل فہم اور منظم بنا کر تعاون بھی بہتر کرتی ہے۔
OOP کے بنیادی تصورات: کلاسز اور آبجیکٹس، انکیپسولیشن، انہیریٹنس اور پولیمورفزم۔ یہ تصورات پروگرامز کو منظم، دوبارہ قابل استعمال اور انتظام میں آسان بناتے ہیں۔
A class is a blueprint or template that defines the properties (attributes) and actions (methods) of an entity. An object is an instance of a class that holds actual values for its attributes. Classes and objects allow programs to model real-world entities in code. Using classes and objects helps organize data and related functionality together, making programs more structured.
Book example — Class: Person: attributes name, age, gender; methods eat(), sleep(), work(). Object P1: name = Bob, age = 34, gender = Male. Object P2: name = Jenny, age = 27, gender = Female. Same class, different data.
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Student Name:", self.name)
# Creating objects
object1 = Student("Ali")
object2 = Student("Ahmed")
# Using objects
object1.display()
object2.display()
Line by line: class Student → defines a blueprint for students. __init__ → sets the name when an object is created. self.name → stores the student's name. display() → prints the student's name. object1 and object2 → two different student objects. Each object has its own data (Ali, Ahmed).
Note: self is the reference to the current object, passed automatically to instance methods.
کلاس ایک بلیو پرنٹ یا ٹیمپلیٹ ہے جو کسی شے کی خصوصیات (ایٹریبیوٹس) اور اعمال (میتھڈز) طے کرتی ہے۔ آبجیکٹ کلاس کی مثال (انسٹینس) ہے جو اپنے ایٹریبیوٹس کے لیے اصل قدریں رکھتا ہے۔ کلاسز اور آبجیکٹس پروگرامز کو حقیقی دنیا کی اشیا کو کوڈ میں ماڈل کرنے کی اجازت دیتے ہیں۔ کلاسز اور آبجیکٹس استعمال کرنے سے ڈیٹا اور متعلقہ فعالیت ایک ساتھ منظم ہوتی ہے، جس سے پروگرام زیادہ منظم ہو جاتے ہیں۔
کتاب کی مثال — کلاس: Person: ایٹریبیوٹس name، age، gender؛ میتھڈز eat()، sleep()، work()۔ آبجیکٹ P1: name = Bob، age = 34، gender = Male۔ آبجیکٹ P2: name = Jenny، age = 27، gender = Female۔ ایک ہی کلاس، مختلف ڈیٹا۔
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Student Name:", self.name)
# آبجیکٹس بنانا
object1 = Student("Ali")
object2 = Student("Ahmed")
# آبجیکٹس استعمال کرنا
object1.display()
object2.display()
سطر بہ سطر: class Student → طلبہ کے لیے بلیو پرنٹ طے کرتی ہے۔ __init__ → آبجیکٹ بنتے وقت نام سیٹ کرتا ہے۔ self.name → طالب علم کا نام محفوظ کرتا ہے۔ display() → طالب علم کا نام پرنٹ کرتا ہے۔ object1 اور object2 → دو مختلف طالب علم آبجیکٹس۔ ہر آبجیکٹ کا اپنا ڈیٹا ہے (Ali، Ahmed)۔
نوٹ: self موجودہ آبجیکٹ کا حوالہ ہے، جو انسٹینس میتھڈز کو خود بخود دیا جاتا ہے۔
Encapsulation is a concept in OOP where we bundle data (variables) and methods (functions) into a single unit (class) and restrict direct access to the data. Unlike languages like Java or C++, Python does not have strict "private" or "protected" keywords. Instead, it relies on naming conventions to signal how data should be accessed. Rule of thumb: hide the data and control access using methods.
Why Encapsulation is important: protects data from unwanted access; improves security; makes code easier to manage; allows controlled modification.
Example: think of a bank account — your balance is hidden. You can access it using deposit() and withdraw(). You cannot directly change the balance. This is encapsulation.
Levels of Access Control: in Python, you can control the visibility of class members using specific naming prefixes:
• Public Members: accessible from anywhere — inside or outside the class. By default, all members in Python are public. Syntax: self.attribute_name
• Protected Members: intended for internal use within the class and its subclasses. It is a convention (not a restriction) to signal to other developers that the member should not be accessed from outside. Syntax: self._attribute_name (single underscore)
• Private Members: highly restricted and cannot be easily accessed from outside the class. Python uses a mechanism called Name Mangling to change the internal name of the variable to prevent accidental access. Syntax: self.__attribute_name (double underscore)
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private variable
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance")
def get_balance(self):
return self.__balance
# Usage
account = BankAccount(1000)
account.deposit(500)
account.withdraw(300)
print(account.get_balance()) # Output: 1200
# Direct access will fail
# print(account.__balance) # Error
انکیپسولیشن OOP کا ایک تصور ہے جس میں ہم ڈیٹا (متغیرات) اور میتھڈز (فنکشنز) کو ایک ہی یونٹ (کلاس) میں باندھتے ہیں اور ڈیٹا تک براہ راست رسائی محدود کرتے ہیں۔ جاوا یا C++ جیسی زبانوں کے برعکس، پائتھون میں سخت "پرائیویٹ" یا "پروٹیکٹڈ" کی ورڈز نہیں ہیں۔ اس کے بجائے یہ نام رکھنے کے رواج پر انحصار کرتا ہے تاکہ بتایا جا سکے کہ ڈیٹا تک کیسے رسائی ہونی چاہیے۔ بنیادی اصول: ڈیٹا چھپاؤ اور میتھڈز سے رسائی کنٹرول کرو۔
انکیپسولیشن کیوں اہم ہے: ڈیٹا کو ناپسندیدہ رسائی سے بچاتی ہے؛ سیکیورٹی بہتر کرتی ہے؛ کوڈ انتظام میں آسان بناتی ہے؛ کنٹرول شدہ ترمیم کی اجازت دیتی ہے۔
مثال: بینک اکاؤنٹ سوچیں — آپ کا بیلنس چھپا ہوا ہے۔ آپ اس تک deposit() اور withdraw() سے رسائی کر سکتے ہیں۔ آپ بیلنس براہ راست تبدیل نہیں کر سکتے۔ یہ انکیپسولیشن ہے۔
ایکسیس کنٹرول کی سطحیں: پائتھون میں آپ مخصوص نام کے پریفکس سے کلاس ممبرز کی نمائش کنٹرول کر سکتے ہیں:
• پبلک ممبرز: کہیں سے بھی قابل رسائی — کلاس کے اندر یا باہر۔ بطور ڈیفالٹ، پائتھون کے تمام ممبرز پبلک ہیں۔ نحو: self.attribute_name
• پروٹیکٹڈ ممبرز: کلاس اور اس کی سب کلاسز کے اندرونی استعمال کے لیے۔ یہ رواج ہے (پابندی نہیں) تاکہ دوسرے ڈویلپرز کو بتایا جائے کہ ممبر باہر سے استعمال نہیں ہونا چاہیے۔ نحو: self._attribute_name (ایک انڈراسکور)
• پرائیویٹ ممبرز: بہت محدود اور کلاس کے باہر سے آسانی سے قابل رسائی نہیں۔ پائتھون نیم مینگلنگ نامی طریقہ استعمال کرتا ہے تاکہ متغیر کا اندرونی نام بدل کر حادثاتی رسائی روکی جا سکے۔ نحو: self.__attribute_name (دو انڈراسکور)
class BankAccount:
def __init__(self, balance):
self.__balance = balance # پرائیویٹ متغیر
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance")
def get_balance(self):
return self.__balance
account = BankAccount(1000)
account.deposit(500)
account.withdraw(300)
print(account.get_balance()) # آؤٹ پٹ: 1200
Inheritance allows a class to take properties and methods from another class. The new class, called a subclass, can add or modify features of the parent class. Inheritance promotes code reuse and reduces redundancy. It also makes it easier to extend programs without rewriting existing code.
Parent Class (Base Class): the class whose properties are inherited. In the example below, Animal is the parent class.
Child Class (Derived Class): the class that inherits from the parent. It is defined by passing the parent class name in parentheses: class Child(Parent).
Method Overriding: a child class can provide a specific implementation of a method that is already provided by its parent. You can use the super() function to call the parent's version of that method.
# Parent class (Superclass)
class Animal:
def __init__(self, name):
self.name = name
def eat(self):
print(f"{self.name} is eating.")
# Child class (Subclass) inheriting from Animal
class Dog(Animal):
def bark(self):
print(f"{self.name} says Woof!")
# Create an object of the child class
my_dog = Dog("Buddy")
# Access method from Parent class
my_dog.eat() # Output: Buddy is eating.
# Access method from Child class
my_dog.bark() # Output: Buddy says Woof!
Single Inheritance: a child class (subclass) inherits attributes and methods from exactly one parent class (superclass). This is the most fundamental form of inheritance in Python, promoting code reusability by allowing the child class to extend or modify the behavior of the parent.
Multiple Inheritance: allows a single class to inherit features (attributes and methods) from more than one parent class. This enables a child class to combine behaviors from different sources, such as a Smartphone class inheriting from both a Camera and a Computer class. Book example: Child(Father, Mother) can access skill1() (Father's skill: Gardening), skill2() (Mother's skill: Cooking), and its own skill own_skill() (Child's skill: Coding).
Multilevel Inheritance: occurs when a class inherits from another derived class, creating a chain of inheritance similar to a family tree (Grandparent → Parent → Child). In this structure, the "grandchild" class has access to all methods and attributes from both its immediate parent and its grandparent. Book example: ElectricCar inherits from Car, which in turn inherits from Vehicle — my_tesla.start_engine() (from Vehicle), my_tesla.drive() (from Car), my_tesla.charge() (defined in ElectricCar).
انہیریٹنس ایک کلاس کو دوسری کلاس سے خصوصیات اور میتھڈز لینے کی اجازت دیتا ہے۔ نئی کلاس، جسے سب کلاس کہتے ہیں، پیرنٹ کلاس کی خصوصیات شامل یا تبدیل کر سکتی ہے۔ انہیریٹنس کوڈ کے دوبارہ استعمال کو فروغ دیتی ہے اور دہراؤ کم کرتی ہے۔ یہ موجودہ کوڈ دوبارہ لکھے بغیر پروگرامز بڑھانا بھی آسان بناتی ہے۔
پیرنٹ کلاس (بیس کلاس): وہ کلاس جس کی خصوصیات وراثت میں ملتی ہیں۔ نیچے کی مثال میں Animal پیرنٹ کلاس ہے۔
چائلڈ کلاس (ڈیرائیوڈ کلاس): وہ کلاس جو پیرنٹ سے وراثت لیتی ہے۔ یہ پیرنٹ کلاس کا نام قوسین میں دے کر بنائی جاتی ہے: class Child(Parent)۔
میتھڈ اووررائیڈنگ: چائلڈ کلاس کسی میتھڈ کی مخصوص شکل دے سکتی ہے جو پیرنٹ پہلے سے دیتا ہے۔ اس میتھڈ کے پیرنٹ ورژن کو بلانے کے لیے super() فنکشن استعمال کر سکتے ہیں۔
# پیرنٹ کلاس
class Animal:
def __init__(self, name):
self.name = name
def eat(self):
print(f"{self.name} is eating.")
# چائلڈ کلاس
class Dog(Animal):
def bark(self):
print(f"{self.name} says Woof!")
my_dog = Dog("Buddy")
my_dog.eat() # آؤٹ پٹ: Buddy is eating.
my_dog.bark() # آؤٹ پٹ: Buddy says Woof!
سنگل انہیریٹنس: چائلڈ کلاس (سب کلاس) بالکل ایک پیرنٹ کلاس (سپر کلاس) سے ایٹریبیوٹس اور میتھڈز وراثت میں لیتی ہے۔ یہ پائتھون میں انہیریٹنس کی سب سے بنیادی شکل ہے، جو چائلڈ کلاس کو پیرنٹ کے رویے کو بڑھانے یا تبدیل کرنے کی اجازت دے کر کوڈ کے دوبارہ استعمال کو فروغ دیتی ہے۔
ملٹی پل انہیریٹنس: ایک کلاس کو ایک سے زیادہ پیرنٹ کلاسز سے خصوصیات (ایٹریبیوٹس اور میتھڈز) وراثت میں لینے کی اجازت دیتی ہے۔ اس سے چائلڈ کلاس مختلف ذرائع کے رویے ملا سکتی ہے، جیسے اسمارٹ فون کلاس جو کیمرہ اور کمپیوٹر دونوں کلاسز سے وراثت لیتی ہے۔ کتاب کی مثال: Child(Father, Mother) skill1() (والد کی مہارت: Gardening)، skill2() (والدہ کی مہارت: Cooking) اور اپنی مہارت own_skill() (بچے کی مہارت: Coding) تک رسائی کر سکتی ہے۔
ملٹی لیول انہیریٹنس: تب ہوتی ہے جب ایک کلاس دوسری ڈیرائیوڈ کلاس سے وراثت لیتی ہے، جس سے خاندانی درخت جیسی وراثت کی زنجیر بنتی ہے (دادا → والد → بچہ)۔ اس ساخت میں "پوتا" کلاس اپنے فوری پیرنٹ اور دادا دونوں کے تمام میتھڈز اور ایٹریبیوٹس تک رسائی رکھتی ہے۔ کتاب کی مثال: ElectricCar، Car سے وراثت لیتی ہے، جو بدلے میں Vehicle سے وراثت لیتی ہے — my_tesla.start_engine() (Vehicle سے)، my_tesla.drive() (Car سے)، my_tesla.charge() (ElectricCar میں)۔
Polymorphism means "many forms". It refers to the ability of an entity (like a function or object) to perform different actions based on the context. Technically, polymorphism allows the same method, function, or operator to behave differently depending on the object it is working with. This makes code more flexible and reusable.
Example: in a backend payment system, multiple payment options are available, such as Credit Card, UPI, NetBanking, and Wallet. All payment types use a common method named processPayment(), but different implementations — e.g. NetBanking Payment redirects to bank login. The method name remains the same, but the action changes based on the payment type.
Types of Polymorphism: mainly compile-time and runtime polymorphism.
1. Compile-time Polymorphism: means deciding which method or operation to run during compilation, usually through method or operator overloading. Languages like Java or C++ support this. But Python does not support true method overloading based on function signatures like statically typed languages (e.g., Java or C++). Because Python is dynamically typed, a single function can accept arguments of different types, making signature-based overloading unnecessary. Instead, similar behavior can be achieved using default arguments, variable-length arguments (*args, **kwargs), or conditional logic inside the function.
class Calculator:
def multiply(self, a=1, b=1, *args):
result = a * b
for num in args:
result *= num
return result
# Create object
calc = Calculator()
# Using default arguments
print(calc.multiply()) # Output: 1
print(calc.multiply(4)) # Output: 4
# Using multiple arguments
print(calc.multiply(2, 3)) # Output: 6
print(calc.multiply(2, 3, 4)) # Output: 24
2. Runtime Polymorphism (Overriding): means that the behavior of a method is decided while the program is running, based on the object calling it. This happens through Method Overriding: a child class provides its own version of a method already defined in the parent class.
class Animal:
def sound(self):
return "Some generic sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
# Polymorphic behavior
animals = [Dog(), Cat(), Animal()]
for animal in animals:
print(animal.sound())
# Output: Bark / Meow / Some generic sound
Here, the sound() method behaves differently depending on whether the object is a Dog, Cat, or Animal, and this decision happens at runtime. This dynamic nature makes Python particularly powerful for runtime polymorphism.
پولیمورفزم کا مطلب "کئی شکلیں" ہے۔ یہ کسی شے (جیسے فنکشن یا آبجیکٹ) کی سیاق و سباق کے لحاظ سے مختلف اعمال کرنے کی صلاحیت ہے۔ تکنیکی طور پر، پولیمورفزم ایک ہی میتھڈ، فنکشن یا آپریٹر کو اس آبجیکٹ کے لحاظ سے مختلف رویہ اختیار کرنے کی اجازت دیتا ہے جس کے ساتھ یہ کام کر رہا ہے۔ اس سے کوڈ زیادہ لچکدار اور دوبارہ قابل استعمال ہوتا ہے۔
مثال: بیک اینڈ ادائیگی کے سسٹم میں ادائیگی کے کئی آپشنز دستیاب ہیں، جیسے کریڈٹ کارڈ، UPI، نیٹ بینکنگ اور والیٹ۔ ادائیگی کی تمام اقسام processPayment() نامی مشترکہ میتھڈ استعمال کرتی ہیں، لیکن مختلف نفاذ کے ساتھ — مثلاً نیٹ بینکنگ ادائیگی بینک لاگ ان پر لے جاتی ہے۔ میتھڈ کا نام ایک ہی رہتا ہے، لیکن عمل ادائیگی کی قسم کے لحاظ سے بدل جاتا ہے۔
پولیمورفزم کی اقسام: بنیادی طور پر کمپائل ٹائم اور رن ٹائم پولیمورفزم۔
1۔ کمپائل ٹائم پولیمورفزم: کا مطلب کمپائلیشن کے دوران یہ فیصلہ کرنا کہ کون سا میتھڈ یا آپریشن چلانا ہے، عام طور پر میتھڈ یا آپریٹر اوورلوڈنگ سے۔ جاوا یا C++ جیسی زبانیں اسے سپورٹ کرتی ہیں۔ لیکن پائتھون اسٹیٹکلی ٹائپڈ زبانوں (جیسے جاوا یا C++) کی طرح فنکشن سگنیچرز کی بنیاد پر حقیقی میتھڈ اوورلوڈنگ سپورٹ نہیں کرتا۔ چونکہ پائتھون ڈائنیمکلی ٹائپڈ ہے، ایک ہی فنکشن مختلف اقسام کے آرگومنٹس قبول کر سکتا ہے، جس سے سگنیچر پر مبنی اوورلوڈنگ غیر ضروری ہو جاتی ہے۔ اس کے بجائے ڈیفالٹ آرگومنٹس، متغیر لمبائی والے آرگومنٹس (*args، **kwargs) یا فنکشن کے اندر مشروط لاجک سے ملتا جلتا رویہ حاصل کیا جا سکتا ہے۔
class Calculator:
def multiply(self, a=1, b=1, *args):
result = a * b
for num in args:
result *= num
return result
calc = Calculator()
print(calc.multiply()) # آؤٹ پٹ: 1
print(calc.multiply(4)) # آؤٹ پٹ: 4
print(calc.multiply(2, 3)) # آؤٹ پٹ: 6
print(calc.multiply(2, 3, 4)) # آؤٹ پٹ: 24
2۔ رن ٹائم پولیمورفزم (اووررائیڈنگ): کا مطلب ہے کہ میتھڈ کا رویہ پروگرام چلتے وقت طے ہوتا ہے، اس آبجیکٹ کی بنیاد پر جو اسے بلا رہا ہے۔ یہ میتھڈ اووررائیڈنگ سے ہوتا ہے: چائلڈ کلاس کسی میتھڈ کا اپنا ورژن دیتی ہے جو پیرنٹ کلاس میں پہلے سے موجود ہے۔
class Animal:
def sound(self):
return "Some generic sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
animals = [Dog(), Cat(), Animal()]
for animal in animals:
print(animal.sound())
# آؤٹ پٹ: Bark / Meow / Some generic sound
یہاں sound() میتھڈ مختلف رویہ اختیار کرتا ہے اس لحاظ سے کہ آبجیکٹ Dog ہے، Cat ہے یا Animal، اور یہ فیصلہ رن ٹائم پر ہوتا ہے۔ یہ متحرک فطرت پائتھون کو رن ٹائم پولیمورفزم کے لیے خاص طور پر طاقتور بناتی ہے۔
Answer key (from the book): 1–C, 2–B, 3–A, 4–A, 5–A, 6–B, 7–B, 8–B, 9–A, 10–B.
Answer key (from the book): 1–C, 2–B, 3–A, 4–A, 5–A, 6–B, 7–B, 8–B, 9–A, 10–B.
Answer: (c) — Encapsulation bundles data and methods into a class and restricts direct access to the data.
Answer: (b) — A class is a blueprint; an object is the real thing created from it.
Answer: (a) — Hide the data and control access using methods.
Answer: (a) — A subclass takes properties and methods from its parent and can add or modify them.
Answer: (a) — "Many forms": the same method behaves differently depending on the object.
Answer: (b) — e.g. class Dog(Animal) inherits eat() from Animal and adds bark().
Answer: (b) — Same method name, different action depending on the object/context.
Answer: (b) — class Student: defines a class; def defines a method inside it.
Answer: (a) — Bundle data + methods into one unit (class) and restrict direct access.
Answer: (b) — A subclass inherits attributes and methods from its parent class.
جوابی کلید (کتاب سے): 1–C، 2–B، 3–A، 4–A، 5–A، 6–B، 7–B، 8–B، 9–A، 10–B۔
1. The OOP concept that hides data inside a class is:
2. Classes and objects in Object-Oriented Programming are best described as:
3. The main idea behind encapsulation in OOP is:
4. Inheritance in OOP means:
5. Polymorphism in OOP is best defined as:
6. An example of inheritance in Python is:
7. The purpose of polymorphism in programming is:
8. The keyword used to define a class in Python is:
9. Encapsulation is best described as:
10. Inheritance allows a class to:
1۔ ڈیٹا کلاس کے اندر چھپانے والا OOP تصور ہے:
2۔ OOP میں کلاسز اور آبجیکٹس کی بہترین وضاحت ہے:
3۔ OOP میں انکیپسولیشن کا بنیادی خیال ہے:
4۔ OOP میں انہیریٹنس کا مطلب ہے:
5۔ OOP میں پولیمورفزم کی بہترین تعریف ہے:
6۔ پائتھون میں انہیریٹنس کی مثال ہے:
7۔ پروگرامنگ میں پولیمورفزم کا مقصد ہے:
8۔ پائتھون میں کلاس طے کرنے کے لیے استعمال ہونے والا کی ورڈ ہے:
9۔ انکیپسولیشن کی بہترین وضاحت ہے:
10۔ انہیریٹنس ایک کلاس کو اجازت دیتا ہے:
1. What is Object-Oriented Programming (OOP)?
A method of designing programs using objects and classes, where each object represents a real-world entity with properties and behaviors, and classes act as templates for creating objects.
2. What is the purpose of a class in Python?
To act as a blueprint or template that defines the properties (attributes) and actions (methods) of an entity, so multiple objects with similar characteristics can be created from it.
3. What is the difference between a class and an object?
A class is a blueprint/template; an object is an instance of a class holding actual values. Example: class Person (attributes name, age, gender) vs Object P1 (Bob, 34, Male) and Object P2 (Jenny, 27, Female).
4. What is purpose of __init__?
__init__ is the constructor method that runs when an object is created; it sets up the object's initial data — e.g. in class Student, __init__(self, name) stores self.name = name.
5. What is encapsulation in OOP, and why is it important?
Bundling data and methods into a single unit (class) and restricting direct access to data. Important because it protects data from unwanted access, improves security, makes code easier to manage, and allows controlled modification.
6. How does encapsulation help in keeping data secure and private?
Data (e.g. a bank balance __balance) is hidden inside the class and can only be reached through methods (deposit(), withdraw(), get_balance()). Direct access from outside fails, so the data cannot be changed without the class's rules (e.g. checking sufficient balance).
7. What is inheritance in OOP, and how does it help in reusing functionality?
A class taking properties and methods from another class (class Child(Parent)). It promotes code reuse and reduces redundancy: the subclass reuses the parent's code and only adds or modifies what is new, instead of rewriting existing code.
8. How does inheritance enable the extension of functionality in Python?
A subclass can add new methods (Dog adds bark() to Animal's eat()) or modify inherited ones (method overriding, using super() to call the parent's version), and inheritance chains (single, multiple, multilevel) extend programs without rewriting code.
9. What is polymorphism in OOP, and how does it make code more flexible?
"Many forms": the same method, function, or operator behaves differently depending on the object/context. Code becomes flexible because one method name (processPayment(), sound()) works for many types, with the action changing per object.
10. How does polymorphism help in creating adaptable and reusable code?
One interface serves many types (e.g. a list of Animal objects: Dog, Cat, Animal each respond to sound() differently at runtime), so new types can be added without changing the code that uses them; in Python, default/*args arguments mimic compile-time overloading, and overriding gives runtime polymorphism.
1. Explain the concept of classes and objects in Object-Oriented Programming (OOP).
A class is a blueprint/template defining attributes (properties) and methods (actions) of an entity; an object is an instance of a class holding actual attribute values. Example — Class: Person (name, age, gender; eat(), sleep(), work()) gives Object P1 (Bob, 34, Male) and Object P2 (Jenny, 27, Female). Code example: class Student with __init__(self, name) and display(); objects Student("Ali") and Student("Ahmed") each hold their own name and display it. Classes and objects organize data and related functionality together, model real-world entities, and make programs structured, modular, and reusable.
2. What is encapsulation in OOP?
Bundling data (variables) and methods (functions) into a single unit (class) and restricting direct access to data. Python has no strict private/protected keywords; it uses naming conventions: public (self.attribute_name, default), protected (self._attribute_name, single underscore — convention only), private (self.__attribute_name, double underscore — name mangling prevents accidental access). Bank account example: __balance is hidden; deposit() and withdraw() control changes (withdraw checks sufficient balance); get_balance() reads it; direct print(account.__balance) fails. Benefits: protects data, improves security, easier management, controlled modification.
3. How does inheritance work in OOP?
A subclass takes properties and methods from a parent (base/super) class by naming it in parentheses — class Child(Parent). The subclass can use inherited members, add new ones, or override (replace) a parent method, calling the parent's version with super(). Examples: Dog(Animal) inherits eat() and adds bark(); Child(Father, Mother) inherits from two parents (multiple); ElectricCar → Car → Vehicle (multilevel chain) can use start_engine(), drive(), and charge(). Inheritance reduces redundancy and extends programs without rewriting code.
4. What are the advantages of using inheritance in Python?
Code reuse (write once, use in subclasses); reduced redundancy and repetition; easier maintenance and updates (change in the parent benefits all subclasses); easier extension without rewriting existing code; better organization (family-tree structure: single, multiple, multilevel); supports polymorphism through overriding; models real-world "is-a" relationships (a Dog is an Animal, an ElectricCar is a Car).
5. What is polymorphism in OOP, and how does it enhance flexibility in code?
"Many forms": the same method/function/operator behaves differently depending on the object or context. Payment example: all payment types share processPayment() but implement it differently (NetBanking redirects to bank login). Runtime example: Animal.sound() returns "Some generic sound", Dog.sound() "Bark", Cat.sound() "Meow" — a loop over [Dog(), Cat(), Animal()] prints each correctly, decided at runtime. Flexibility: one method name works for many types, so code handles new types without change; Python mimics compile-time overloading with default arguments and *args (Calculator.multiply gives 1, 4, 6, 24).
6. How can encapsulation help improve data security in OOP?
By hiding data inside the class and forcing all access through methods that enforce rules: the BankAccount's __balance cannot be read or changed directly (name mangling blocks accidental access); deposit() and withdraw() are the only paths, and withdraw() refuses amounts above the balance ("Insufficient balance"); get_balance() allows controlled reading. So invalid states are impossible from outside, unwanted access is prevented, and security improves while the public interface (getValue/setValue style methods) stays simple.
7. How does the combination of inheritance, encapsulation, and polymorphism contribute to better software design?
Together they give the three OOP pillars of good design: encapsulation bundles data with its methods and hides internals, so each class is a safe, understandable module; inheritance reuses and extends those modules (Animal → Dog, Vehicle → Car → ElectricCar) without duplication; polymorphism lets one interface serve many forms (sound(), processPayment()) so code stays flexible and adaptable. Result: modular, reusable, maintainable, scalable programs that are easier to update (change in one part does not affect the whole system) and easier for teams to collaborate on — exactly the benefits the chapter lists for OOP.
8. What are the key differences between a class and an object in Object-Oriented Programming?
Class: a blueprint/template; defines attributes and methods; does not hold actual data itself; one class can create many objects; defined with the class keyword (class Student:). Object: an instance of a class; holds actual values for attributes (Ali vs Ahmed); created by calling the class (Student("Ali")); each object has its own data but shares the class's methods; objects are the "real things" that do the work at runtime, while the class is only the design.
This short chapter is the heart of Python OOP: classes and objects, encapsulation, inheritance and polymorphism. The ideas are few but deep, so learn each one with its code pattern — a class definition, a private attribute, a child class, and an overridden method — and the MCQs become easy.
یہ چھوٹا باب Python OOP کا دل ہے — classes اور objects, encapsulation, inheritance اور polymorphism. Ideas کم مگر گہرے ہیں, اس لیے ہر ایک کو اس کے code pattern کے ساتھ سیکھو — class definition, private attribute, child class, اور overridden method — پھر MCQs آسان ہو جائیں گے.
Revision guidance: Revise in this order: (1) read each section and say its idea in one sentence, (2) cover the answers and redo all 20 MCQs, (3) write the 10 short answers from memory, then (4) plan — not just read — two long answers. Revisit any section whose figure you cannot explain without looking.
دہرائی کی رہنمائی: اس ترتیب سے revise کرو: (1) ہر section پڑھ کر اسکا idea ایک جملے میں بتاؤ, (2) جواب چھپا کر 20 MCQs دوبارہ حل کرو, (3) 10 short answers ذہن سے لکھو, پھر (4) دو long answers صرف پڑھنا نہیں, ان کا plan بناؤ. جس section کی figure بغیر دیکھے سمجہ نہ سکو, اسے دوبارہ پڑھو.
Chapter-wise tests, fresh random papers اور instant score کے لیے PunjabTest.com کا میں test section دیکھو:
PunjabTest.com — Complete Guide Book • FSc Part 2 Computer Science, Chapter 3 (New Book, Session 2026–27) • English + Urdu