0% found this document useful (0 votes)
3 views

oop

The document provides a comprehensive list of common Object-Oriented Programming (OOP) interview questions along with examples in both English and Urdu. It covers key concepts such as Encapsulation, Inheritance, Polymorphism, and Abstraction, explaining each with code examples in various programming languages. Additionally, it includes explanations of Method Overloading and Method Overriding.

Uploaded by

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

oop

The document provides a comprehensive list of common Object-Oriented Programming (OOP) interview questions along with examples in both English and Urdu. It covers key concepts such as Encapsulation, Inheritance, Polymorphism, and Abstraction, explaining each with code examples in various programming languages. Additionally, it includes explanations of Method Overloading and Method Overriding.

Uploaded by

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

Here is a list of common Object-Oriented Programming (OOP) interview questions with examples

provided in both English and Urdu. The Urdu text is written in the local Urdu script.

1. What is Object-Oriented Programming (OOP)?

English:
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects,"
which can contain data (attributes) and methods (functions).

Example in PHP:

class Car {

public $color;

public $model;

public function __construct($color, $model) {

$this->color = $color;

$this->model = $model;

public function displayInfo() {

return "This is a $this->color $this->model.";

$car = new Car('Red', 'Toyota');

echo $car->displayInfo();

Urdu:
‫( آبجیکٹ اورینٹڈ پروگرامنگ‬OOP) ‫می ڈیٹا‬ ‫ایک پروگرامنگ رپیاڈائم ہ جو "آبجیکٹس" ےک تصور پر ی‬
‫مبن ےہ۔ ان ر‬ ‫ے‬
‫ے‬
(attributes) ‫( اور میتھڈز‬functions) ‫شامل ہوت رہی۔‬

‫مثال‬:

class Car {

public $color;

public $model;
public function __construct($color, $model) {

$this->color = $color;

$this->model = $model;

public function displayInfo() {

return "‫ یہ ایک‬$this->color $this->model ‫;"ہ۔‬


‫ے‬
}

$car = new Car('Red', 'Toyota');

echo $car->displayInfo();

2. What is a Class and an Object?

English:
A class is a blueprint for creating objects, and an object is an instance of a class.

Example in Python:

class Person:

def __init__(self, name, age):

self.name = name

self.age = age

def greet(self):

return f"Hello, my name is {self.name} and I am {self.age} years old."

person = Person("Ali", 25)

print(person.greet())

Urdu:
‫( کالس ایک بلیو پرنٹ ہ جو آبجیکٹس بنا ی‬instance) ‫ہ۔‬
‫ اور آبجیکٹ ایک کالس یک ایک مثال‬،‫ن ےک رلی استعمال ہوتا ےہ‬ ‫ے‬ ‫ے‬
‫مثال‬:
class Person:

def __init__(self, name, age):

self.name = name

self.age = age

def greet(self):

return f"‫میا نام‬


‫ ر‬،‫{ ہیلو‬self.name} ‫میی عمر‬
‫{ ےہ اور ر‬self.age} ‫"سال ےہ۔‬

person = Person("‫"عل‬, 25)

print(person.greet())

3. What is Inheritance in OOP?

English:
Inheritance is a mechanism where one class can inherit properties and methods from another class.

Example in Java:

class Animal {

void eat() {

System.out.println("This animal eats food.");

class Dog extends Animal {

void bark() {

System.out.println("The dog barks.");

public class Main {

public static void main(String[] args) {

Dog dog = new Dog();


dog.eat();

dog.bark();

Urdu:
‫ے‬
‫طریق‬ ‫( وراثت ایک میکانزم ےہ جس ےک ذریےع ایک کالس دورسی کالس یک خصوصیات اور‬methods) ‫می حاصل کر‬
‫وراثت ر‬
‫ے‬
‫سکن ےہ۔‬

‫مثال‬:

class Animal {

void eat() {

System.out.println("‫;)"یہ جانور کھاتا ےہ۔‬

class Dog extends Animal {

void bark() {

System.out.println("‫;)"کتا بھونکتا ےہ۔‬

public class Main {

public static void main(String[] args) {

Dog dog = new Dog();

dog.eat();

dog.bark();

4. What is Polymorphism in OOP?

English:
Polymorphism allows objects to take multiple forms. It provides a way to perform a single action in
different ways.

Example in C#:
using System;

class Shape {

public virtual void Draw() {

Console.WriteLine("Drawing a shape.");

class Circle : Shape {

public override void Draw() {

Console.WriteLine("Drawing a circle.");

class Program {

static void Main() {

Shape shape = new Circle();

shape.Draw();

Urdu:
‫کو مختلف شکلی اختیار ی‬
‫( پویل مورفزم ایک تصور ےہ جو اشیاء‬objects) ‫کرت یک اجازت دیتا ےہ۔ یہ مختلف طریقوں ےس ایک‬ ‫ر‬
‫ی‬
‫یہ عمل انجام دین کا طریقہ فراہم کرتا ےہ۔‬

‫مثال‬:

csharp

Copy code

using System;

class Shape {
public virtual void Draw() {

Console.WriteLine("‫;)"ایک شکل کھینچ رہا ےہ۔‬

class Circle : Shape {

public override void Draw() {

Console.WriteLine("‫;)"ایک دائرہ کھینچ رہا ےہ۔‬

class Program {

static void Main() {

Shape shape = new Circle();

shape.Draw();

5. What is Encapsulation?

English:
Encapsulation is the practice of keeping fields within a class private, then providing access to them via
public methods.

Example in Python:

python

Copy code

class BankAccount:

def __init__(self, balance):

self.__balance = balance
def deposit(self, amount):

self.__balance += amount

def get_balance(self):

return self.__balance

account = BankAccount(1000)

account.deposit(500)

print(account.get_balance())

Urdu:
‫ے‬
‫رسائ عوایم‬ ‫( انسکیپسولیشن کا مطلب ہ کہ کالس ےک اندر موجود فیلڈز کو پرائیویٹ رکھا جا ے‬public) ‫میتھڈز ےک‬
‫ن اور ان تک‬ ‫ے‬
‫ن۔‬‫ذریےع فراہم یک جا ے‬

‫مثال‬:

python

Copy code

class BankAccount:

def __init__(self, balance):

self.__balance = balance

def deposit(self, amount):

self.__balance += amount

def get_balance(self):

return self.__balance

account = BankAccount(1000)

account.deposit(500)

print(account.get_balance())
6. What is Abstraction in OOP?

English:
Abstraction is the process of hiding the implementation details and showing only the essential
features.

Example in Java:

java

Copy code

abstract class Animal {

abstract void sound();

class Cat extends Animal {

void sound() {

System.out.println("Meow");

public class Main {

public static void main(String[] args) {

Animal animal = new Cat();

animal.sound();

Urdu:
‫دکھائ ے‬
‫جائ رہی۔‬ ‫ے‬ ‫چھپائ ے‬
‫جائ رہی اور رصف یرصوری خصوصیات‬ ‫ے‬ ‫می تفصیالت‬ ‫ر‬
‫ابسییکشن وہ عمل ےہ جس ر‬
‫مثال‬:

java

Copy code

abstract class Animal {

abstract void sound();


}

class Cat extends Animal {

void sound() {

System.out.println("‫;)"میاؤں‬

public class Main {

public static void main(String[] args) {

Animal animal = new Cat();

animal.sound();

Let me know if you'd like more examples or clarification!

4o

You said:

more aslo with oop pillers

ChatGPT said:

ChatGPT

Here’s an expanded list of Object-Oriented Programming (OOP) interview questions, examples, and
explanations with a focus on the four pillars of OOP. All examples are provided in English and Urdu.

Four Pillars of OOP:

1. Encapsulation (‫)انسکیپسولیشن‬

2. Inheritance (‫)وراثت‬

3. Polymorphism (‫)پویل مورفزم‬


‫ر‬
4. Abstraction (‫)ابسییکشن‬
1. What is Encapsulation? Why is it used?

English:
Encapsulation is the concept of bundling data (fields) and methods (functions) that operate on the
data into a single unit (class). It restricts direct access to some components of the object to maintain
control over the data.

Example in Python:

python

Copy code

class Employee:

def __init__(self, name, salary):

self.__name = name # Private attribute

self.__salary = salary # Private attribute

def set_salary(self, salary):

if salary > 0:

self.__salary = salary

def get_salary(self):

return self.__salary

employee = Employee("Ali", 50000)

employee.set_salary(55000)

print(employee.get_salary())

Urdu:
‫ے‬
‫رسائ کو‬ ‫می اکٹھا کرنا ےہ۔ یہ ڈیٹا ےک براہ راست‬‫انسکیپسولیشن کا مطلب ڈیٹا (فیلڈز) اور میتھڈز کو ایک واحد یونٹ (کالس) ر‬
‫کنیول برقرار ر ی‬
‫کھن ےک رلی استعمال کیا جاتا ےہ۔‬ ‫محدود کرےک ر‬

‫مثال‬:

python

Copy code
class Employee:

def __init__(self, name, salary):


‫پرائیویٹ ر‬
self.__name = name # ‫ایییبیوٹ‬
‫پرائیویٹ ر‬
self.__salary = salary # ‫ایییبیوٹ‬

def set_salary(self, salary):

if salary > 0:

self.__salary = salary

def get_salary(self):

return self.__salary

employee = Employee("‫"عل‬, 50000)

employee.set_salary(55000)

print(employee.get_salary())

2. What is Inheritance? Why is it important?

English:
Inheritance allows a class (child class) to inherit attributes and methods from another class (parent
class). It promotes code reuse and establishes a parent-child relationship between classes.

Example in Java:

java

Copy code

class Vehicle {

void move() {

System.out.println("This vehicle moves.");

}
class Car extends Vehicle {

void drive() {

System.out.println("The car is driving.");

public class Main {

public static void main(String[] args) {

Car car = new Car();

car.move(); // Inherited method

car.drive(); // Child class method

Urdu:
‫لین یک اجازت ے‬
‫دین ےہ۔‬ ‫می ی‬‫(پینٹ کالس) یک خصوصیات اور میتھڈز وراثت ر‬
‫وراثت ایک کالس (چائلڈ کالس) کو دورسی کالس ر‬
‫ی‬
‫یہ کوڈ کو دوبارہ استعمال کرت ےک رلی اہم ےہ۔‬

‫مثال‬:

java

Copy code

class Vehicle {

void move() {
‫;)"یہ گاڑی حرکت ے‬
System.out.println("‫کرئ ےہ۔‬

class Car extends Vehicle {

void drive() {

System.out.println("‫;)"گاڑی چال ریہ ےہ۔‬

}
}

public class Main {

public static void main(String[] args) {

Car car = new Car();

car.move(); // ‫وراثت شدہ میتھڈ‬

car.drive(); // ‫چائلڈ کالس میتھڈ‬

3. What is Polymorphism? How is it implemented?

English:
Polymorphism means "many forms." It allows one interface to be used for a general class of actions.
Polymorphism can be achieved using method overriding and method overloading.

Example in C#:

csharp

Copy code

using System;

class Shape {

public virtual void Draw() {

Console.WriteLine("Drawing a shape.");

class Rectangle : Shape {

public override void Draw() {

Console.WriteLine("Drawing a rectangle.");

}
}

class Program {

static void Main() {

Shape shape = new Rectangle(); // Runtime polymorphism

shape.Draw();

Urdu:
‫ی‬
‫کرت یک اجازت دیتا ےہ۔‬ ‫پویل مورفزم کا مطلب "بہت یس شکلی" ہی۔ یہ ایک ر‬
‫انیفیس کو ے‬
‫کن مختلف کاموں ےک رلی استعمال‬ ‫ر‬ ‫ر‬
‫مثال‬:

csharp

Copy code

using System;

class Shape {

public virtual void Draw() {

Console.WriteLine("‫;)"ایک شکل کھینچ رہا ےہ۔‬

class Rectangle : Shape {

public override void Draw() {

Console.WriteLine("‫;)"ایک مستطیل کھینچ رہا ےہ۔‬

class Program {

static void Main() {


Shape shape = new Rectangle(); // ‫رن ٹائم پویل مورفزم‬

shape.Draw();

4. What is Abstraction? How is it achieved?

English:
Abstraction is the process of hiding implementation details and showing only essential features to the
user. It can be achieved using abstract classes or interfaces.

Example in Java:

java

Copy code

abstract class Animal {

abstract void sound(); // Abstract method

class Dog extends Animal {

void sound() {

System.out.println("Bark");

public class Main {

public static void main(String[] args) {

Animal animal = new Dog();

animal.sound();

}
Urdu:
‫کالرس یا ر‬
‫انیفیس ےک‬ ‫ی‬ ‫ر‬
‫ایبسییکٹ‬ ‫ر‬
‫ابسییکشن کا مطلب ےہ تفصیالت کو چھپانا اور رصف یرصوری خصوصیات کو ظاہر کرنا۔ یہ‬
‫ذریےع حاصل کیا جا سکتا ےہ۔‬

‫مثال‬:

java

Copy code

abstract class Animal {


‫ر‬
abstract void sound(); // ‫ایبسییکٹ میتھڈ‬

class Dog extends Animal {

void sound() {

System.out.println("‫;)"بھونک‬

public class Main {

public static void main(String[] args) {

Animal animal = new Dog();

animal.sound();

5. Explain Method Overloading and Method Overriding.

Method Overloading:

English:
Creating multiple methods in the same class with the same name but different parameters.

Example in Python:

python

Copy code
class Calculator:

def add(self, a, b):

return a + b

def add(self, a, b, c=0):

return a + b + c

calc = Calculator()

print(calc.add(5, 10)) # Output: 15

print(calc.add(5, 10, 20)) # Output: 35

Urdu:
‫ایک یہ کالس می ایک یہ نام ےک ساتھ لیکن مختلف پی ر‬
‫امییز ےک ساتھ متعدد میتھڈز بنانا۔‬ ‫ر‬ ‫ر‬
‫مثال‬:

python

Copy code

class Calculator:

def add(self, a, b):

return a + b

def add(self, a, b, c=0):

return a + b + c

calc = Calculator()

print(calc.add(5, 10)) # 15 :‫نتیجہ‬

print(calc.add(5, 10, 20)) # 35 :‫نتیجہ‬

Method Overriding:

English:
Redefining a method in a child class that already exists in the parent class.

Example in Java:
java

Copy code

class Parent {

void greet() {

System.out.println("Hello from the Parent class.");

class Child extends Parent {

void greet() {

System.out.println("Hello from the Child class.");

public class Main {

public static void main(String[] args) {

Parent parent = new Child();

parent.greet();

Urdu:
‫می پہےل ےس موجود ہو۔‬
‫می اس میتھڈ کو دوبارہ ڈیفائن کرنا جو رپینٹ کالس ر‬
‫ایک چائلڈ کالس ر‬
‫مثال‬:

java

Copy code

class Parent {

void greet() {

System.out.println("‫;)"پینٹ کالس ےس ہیلو۔‬


‫ر‬
}
}

class Child extends Parent {

void greet() {

System.out.println("‫;)"چائلڈ کالس ےس ہیلو۔‬

public class Main {

public static void main(String[] args) {

Parent parent = new Child();

parent.greet();

Let me know if you need more examples or explanations!

4o

You said:

more about oop

ChatGPT said:

ChatGPT

Object-Oriented Programming (OOP) Detailed Overview

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects."


Objects are instances of classes and are used to model real-world entities. OOP provides a structured
way to write modular, reusable, and scalable code.

Key Features of OOP:

1. Class (‫)کالس‬

• A class is a blueprint for creating objects.


• It defines properties (attributes) and methods (functions) that objects instantiated from the
class will have.

Example (Python):

python

Copy code

class Car:

def __init__(self, brand, model):

self.brand = brand

self.model = model

def drive(self):

print(f"{self.brand} {self.model} is driving.")

# Creating objects

car1 = Car("Toyota", "Corolla")

car1.drive()

Urdu:
‫کالس ایک بلوپرنٹ ہ جو آبجیکٹس بنا ی‬
‫ن ےک رلی استعمال ہوتا ےہ۔‬ ‫ے‬

2. Object (‫)آبجیکٹ‬

• An object is an instance of a class.

• It is a real-world entity that has states (attributes) and behaviors (methods).

Example:

python

Copy code

car1 = Car("Honda", "Civic") # Object creation

car2 = Car("Suzuki", "Swift")

Urdu:
‫آبجیکٹ ایک کالس کا انسٹینس ےہ جو اصل دنیا ےک وجود کو ظاہر کرتا ےہ۔‬
‫)کنسی ر‬
3. Constructor (‫کی‬ ‫ر‬

• A special method used to initialize objects when a class is instantiated.

• Typically named __init__ in Python or the class name in Java/C++.

Example (C++):

cpp

Copy code

#include <iostream>

using namespace std;

class Car {

public:

string brand;

string model;

Car(string b, string m) { // Constructor

brand = b;

model = m;

void display() {

cout << "Brand: " << brand << ", Model: " << model << endl;

};

int main() {

Car car1("Honda", "Civic");

car1.display();

return 0;

}
Urdu:
‫کرت ےک وقت آبجیکٹس کو انیشالئز ی‬
‫کرت ےک رلی استعمال ہوتا ےہ۔‬ ‫ی‬ ‫کنسی ر‬
‫کی ایک خاص میتھڈ ےہ جو کالس کو انسٹیٹ‬ ‫ر‬

OOP Principles with Detailed Examples

1. Encapsulation (‫)انسکیپسولیشن‬

• Binding data and methods together in a class.

• Access to the data is controlled through getter and setter methods.

Example (Java):

java

Copy code

class BankAccount {

private double balance;

public void setBalance(double amount) {

if (amount > 0) {

this.balance = amount;

public double getBalance() {

return balance;

public class Main {

public static void main(String[] args) {

BankAccount account = new BankAccount();

account.setBalance(5000);

System.out.println("Balance: " + account.getBalance());


}

Urdu:
‫کنیول یک ے‬
‫جائ ےہ۔‬ ‫سیی ےک ذریےع ر‬
‫گیی اور ر‬ ‫ے‬
‫رسائ ر‬ ‫ی‬
‫جوڑت کا عمل ےہ۔ ڈیٹا تک‬ ‫انسکیپسولیشن ڈیٹا اور میتھڈز کو ایک ساتھ‬

2. Inheritance (‫)وراثت‬

• A mechanism to allow one class to acquire the properties and methods of another class.

• The parent class is called the base class, and the child class is called the derived class.

Example (C++):

cpp

Copy code

#include <iostream>

using namespace std;

class Animal {

public:

void eat() {

cout << "This animal eats food." << endl;

};

class Dog : public Animal {

public:

void bark() {

cout << "The dog barks." << endl;

};

int main() {
Dog dog;

dog.eat(); // Inherited method

dog.bark();

return 0;

Urdu:
‫می استعمال کرنا۔‬
‫وراثت کا مطلب ےہ ایک کالس یک خصوصیات اور میتھڈز کو دورسی کالس ر‬

3. Polymorphism (‫)پویل مورفزم‬

• The ability of an object to take on many forms.

• Achieved through method overloading (compile-time) and method overriding (run-time).

Example (C++):

cpp

Copy code

#include <iostream>

using namespace std;

class Shape {

public:

virtual void draw() {

cout << "Drawing a shape" << endl;

};

class Circle : public Shape {

public:

void draw() override {

cout << "Drawing a circle" << endl;

}
};

int main() {

Shape *shape = new Circle(); // Run-time polymorphism

shape->draw();

return 0;

Urdu:
‫پویل مورفزم کا مطلب ہ ایک ر‬
‫انیفیس ےک ذریےع مختلف اشکال کو ظاہر کرنا۔‬ ‫ے‬

‫ر‬
4. Abstraction (‫)ابسییکشن‬

• Hiding unnecessary details and showing only essential features of an object.

• Achieved using abstract classes or interfaces.

Example (Python):

python

Copy code

from abc import ABC, abstractmethod

class Animal(ABC):

@abstractmethod

def sound(self):

pass

class Dog(Animal):

def sound(self):

print("Bark")

class Cat(Animal):

def sound(self):
print("Meow")

dog = Dog()

cat = Cat()

dog.sound()

cat.sound()

Urdu:
‫غی یرصوری تفصیالت کو چھپانا اور رصف یرصوری خصوصیات کو ظاہر کرنا۔‬ ‫ر‬
‫ابسییکشن کا مطلب ےہ ر‬

Advanced OOP Concepts

1. Association (‫)ایسویس ایشن‬:

• A relationship between two classes where one class uses the other.

• Example: A "Student" class is associated with a "School" class.

2. Aggregation (‫)ایگریگیشن‬:

• A "has-a" relationship where one class contains another class object.

• Example: A "Library" class contains multiple "Book" objects.

3. Composition (‫)کمپوزیشن‬:

• A strong form of aggregation where the contained object cannot exist independently of the
container.

• Example: A "Car" class contains an "Engine" object.

Interview Questions for OOP

1. What is the difference between a class and an object?

o Urdu: ‫می کیا فرق ےہ؟‬


‫کالس اور آبجیکٹ ر‬
2. Explain the concept of method overloading and overriding.

o Urdu: ‫میتھڈ اوورلوڈنگ اور اووررائڈنگ ےک تصور یک وضاحت کریں۔‬

3. How do abstraction and encapsulation differ?

o Urdu: ‫می کیا فرق ےہ؟‬ ‫ر‬


‫ابسییکشن اور انسکیپسولیشن ر‬
4. What are access modifiers?
o Urdu: ‫ایکسس ماڈیفائرز کیا رہی؟‬

5. What is the purpose of constructors?

o
‫کنسی ر‬
Urdu: ‫کیز کا مقصد کیا ےہ؟‬ ‫ر‬

6. How is polymorphism implemented in your preferred language?

o Urdu: ‫می کیےس نافذ کیا جاتا ےہ؟‬


‫پویل مورفزم کو آپ یک پسندیدہ زبان ر‬
7. What is the significance of this keyword in OOP?

o Urdu: ‫می‬
‫او او یئ ر‬this ‫کیورڈ یک اہمیت کیا ےہ؟‬

Real-Life OOP Applications

1. Banking System:

o Classes: Account, Customer, Transaction.

o Encapsulation: Secure data like account balance.

o Inheritance: SavingsAccount and CheckingAccount.

2. E-Commerce System:

o Classes: Product, Cart, User.

o Polymorphism: Different payment methods (CreditCard, PayPal).

3. Game Development:

o Classes: Player, Enemy, Weapon.

o Abstraction: Hiding implementation of attack behavior.

Let me know if you need more examples or details!

4o

You said:

more

ChatGPT said:

ChatGPT

Comprehensive Insights into OOP Concepts

Here’s a deeper dive into Object-Oriented Programming (OOP), including more examples, practical
applications, and advanced topics in English and Urdu for better understanding.
Core OOP Features with Examples

1. Object (‫)آبجیکٹ‬

• Objects represent real-world entities and have states (attributes) and behaviors (methods).

• Example: A car has a color, model, and the ability to drive.

Example (Python):

python

Copy code

class Car:

def __init__(self, brand, color):

self.brand = brand

self.color = color

def drive(self):

print(f"{self.color} {self.brand} is now driving.")

# Creating objects

car1 = Car("Toyota", "Red")

car1.drive()

Urdu:
‫آبجیکٹ کالس کا انسٹینس ہوتا ہ جو اصل دنیا یک ر ی‬
‫ جیےس کار۔‬،‫چیوں کو ظاہر کرتا ےہ‬ ‫ے‬

2. Class (‫)کالس‬

• A blueprint or template for creating objects.

• Example: A "Car" class defines attributes like brand and model and methods like drive.

Example (C++):

cpp

Copy code

#include <iostream>
using namespace std;

class Car {

public:

string brand;

string color;

void drive() {

cout << color << " " << brand << " is driving." << endl;

};

int main() {

Car car1;

car1.brand = "Honda";

car1.color = "Blue";

car1.drive();

return 0;

Urdu:
‫ن جا ے‬
‫ن رہی۔‬ ‫کالس ایک ایسا ڈھانچہ ہ جس ےک ذریےع آبجیکٹس بنا ے‬
‫ے‬

OOP Principles Revisited with Scenarios

1. Encapsulation (‫)انسکیپسولیشن‬

• Definition: Bundling of data and methods that operate on the data, restricting direct access.

• Scenario: A banking system where only authorized methods can modify an account's balance.

Example (Java):

java

Copy code
class BankAccount {

private double balance;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

public double getBalance() {

return balance;

Urdu:
‫ے‬
‫رسائ کو محدود کرنا۔‬ ‫انسکیپسولیشن کا مطلب ےہ ڈیٹا اور میتھڈز کو ایک ساتھ باندھنا اور ان تک براہ راست‬

2. Inheritance (‫)وراثت‬

• Definition: Allows a class to use the properties and methods of another class.

• Scenario: A vehicle class (parent) has properties like speed, which a car class (child) can inherit
and extend.

Example (Python):

python

Copy code

class Vehicle:

def __init__(self, speed):

self.speed = speed

def display_speed(self):

print(f"Speed: {self.speed} km/h")


class Car(Vehicle):

def __init__(self, speed, brand):

super().__init__(speed)

self.brand = brand

def show_details(self):

print(f"Brand: {self.brand}")

self.display_speed()

car = Car(120, "Toyota")

car.show_details()

Urdu:
‫دین ےہ۔‬ ‫ی‬
‫کرت ے‬ ‫وراثت ایک کالس کو دورسی کالس یک خصوصیات اور میتھڈز استعمال‬

3. Polymorphism (‫)پویل مورفزم‬

• Definition: One interface, multiple implementations.

• Scenario: Different payment methods (credit card, PayPal, etc.) in an e-commerce platform.

Example (Java):

java

Copy code

class Animal {

public void sound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

public void sound() {


System.out.println("Dog barks");

class Cat extends Animal {

public void sound() {

System.out.println("Cat meows");

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog();

Animal myCat = new Cat();

myDog.sound();

myCat.sound();

Urdu:
‫ی‬
‫کرت یک اجازت دیتا ےہ۔‬ ‫پویل مورفزم ایک ر‬
‫انیفیس ےک ذریےع مختلف طریقوں ےس کام‬

‫ر‬
4. Abstraction (‫)ابسییکشن‬

• Definition: Hiding complex implementation details and exposing only the essential features.

• Scenario: A user interacts with a car's steering and pedals without knowing the internal
mechanism.

Example (C++):

cpp

Copy code
#include <iostream>

using namespace std;

class Vehicle {

public:

virtual void start() = 0; // Pure virtual function

};

class Car : public Vehicle {

public:

void start() override {

cout << "Car is starting..." << endl;

};

int main() {

Vehicle *v = new Car();

v->start();

delete v;

return 0;

Urdu:
‫غی یرصوری تفصیالت چھپانا۔‬ ‫ر‬
‫ابسییکشن کا مطلب ہ رصف یرصوری ر ی‬
‫چییں دکھانا اور ر‬ ‫ے‬

Advanced OOP Concepts

1. Composition (‫)کمپوزیشن‬

• Composition means "has-a" relationship, where one object contains another.

Example:

python
Copy code

class Engine:

def __init__(self, horsepower):

self.horsepower = horsepower

class Car:

def __init__(self, brand, engine):

self.brand = brand

self.engine = engine

engine = Engine(150)

car = Car("Toyota", engine)

print(f"{car.brand} has an engine with {car.engine.horsepower} HP.")

Urdu:
‫کمپوزیشن کا مطلب ہ کہ ایک کالس دورسی کالس ےک آبجیکٹ کو شامل ے‬
‫کرئ ےہ۔‬ ‫ے‬

2. Aggregation (‫)ایگریگیشن‬

• A weaker form of composition. The contained object can exist independently.

Example:

java

Copy code

class Department {

String name;

Department(String name) {

this.name = name;

}
class University {

String universityName;

List<Department> departments;

University(String universityName, List<Department> departments) {

this.universityName = universityName;

this.departments = departments;

Urdu:
‫ایگریگیشن ایک ہلکا کمپوزیشن ےہ جہاں شامل کردہ آبجیکٹ آزاد رہ سکتا ےہ۔‬

OOP in Real-World Applications

1. E-Commerce System:

o Classes: Product, Cart, Payment.

o Example: Encapsulation ensures that a user's payment details are hidden.

2. Banking System:

o Classes: Account, Customer, Transaction.

o Example: Abstraction hides the implementation of transaction processing.

3. Hospital Management:

o Classes: Patient, Doctor, Appointment.

o Example: Polymorphism allows multiple ways to schedule appointments.

4. Game Development:

o Classes: Player, Enemy, Weapon.

o Example: Inheritance allows multiple enemy types to share common behavior.

Interview Questions

1. How does inheritance differ from composition?


Urdu: ‫می کیا فرق ےہ؟‬
‫وراثت اور کمپوزیشن ر‬
2. What are abstract classes and interfaces? How are they different?
Urdu: ‫می کیا فرق ےہ؟‬ ‫ر‬ ‫ی‬ ‫ر‬
‫کالرس اور انیفیس کیا رہی؟ ان ر‬ ‫ابسییکٹ‬

3. Why is OOP preferred over procedural programming?


‫او او ئ کو پراسیجرل پروگرامنگ پر کیوں ترجیح دی ے‬
Urdu: ‫جائ ےہ؟‬ ‫ی‬
4. What is method overriding, and when would you use it?
Urdu: ‫میتھڈ اووررائڈنگ کیا ےہ اور اےس کب استعمال کیا جاتا ےہ؟‬

5. Explain the significance of design patterns in OOP.


‫او او ئ می ڈیزائن ر‬
Urdu: ‫پیینز یک اہمیت بیان کریں۔‬ ‫ی ر‬

You might also like