0% found this document useful (0 votes)
41 views3 pages

Five important long question of oop

Important long question for final term

Uploaded by

YOUSAF KULACHI
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)
41 views3 pages

Five important long question of oop

Important long question for final term

Uploaded by

YOUSAF KULACHI
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/ 3

Here are 5 important questions and solutions related to Object-Oriented Programming (OOP),

including one program.

### 1. **What are the main principles of Object-Oriented Programming?**

The four main principles of OOP are:

- **Encapsulation**: The concept of wrapping data (variables) and the methods (functions) that
operate on the data into a single unit called a class.

- **Abstraction**: Hiding the complex implementation details and showing only the essential
features of the object.

- **Inheritance**: The ability to create a new class (derived class) from an existing class (base
class), inheriting attributes and methods.

- **Polymorphism**: The ability of a single function, method, or operator to work in different ways
based on the objects involved.

---

### 2. **What is the difference between a class and an object in OOP?**

- **Class**: A class is a blueprint or prototype for creating objects. It defines attributes


(variables) and behaviors (methods) that the objects created from the class will have.

- **Object**: An object is an instance of a class. When a class is defined, no memory is


allocated. The object represents the actual entity that occupies memory.

---

### 3. **What is the concept of constructor and destructor in OOP?**

- **Constructor**: A constructor is a special method used to initialize objects. It is called


automatically when an object of a class is created. Its primary purpose is to initialize the object.

- **Destructor**: A destructor is a special method used to clean up when an object is no longer


in use. It is called when an object is destroyed or goes out of scope.

---

### 4. **What is polymorphism in OOP?**


Polymorphism is the ability of one method, function, or operator to behave differently based on
the object or data type involved. There are two types of polymorphism:

- **Compile-time Polymorphism (Method Overloading)**: Occurs when two or more methods


have the same name but differ in parameters (number or type).

- **Runtime Polymorphism (Method Overriding)**: Occurs when a method in a derived class


overrides a method in a base class with the same name and signature.

---

### 5. **Write a simple program that demonstrates inheritance, polymorphism, and


encapsulation in OOP.**

Here is a simple Python program that demonstrates inheritance, polymorphism, and


encapsulation:

```python
# Parent class
class Animal:
def __init__(self, name, age):
self.name = name # Encapsulation: Attribute (name) is encapsulated
self.age = age # Encapsulation: Attribute (age) is encapsulated

def speak(self): # Method to be overridden


pass

# Child class 1
class Dog(Animal):
def __init__(self, name, age, breed):
super().__init__(name, age) # Inheritance: Inheriting from Animal class
self.breed = breed

def speak(self): # Polymorphism: Method overriding


return f"{self.name} says Woof!"

# Child class 2
class Cat(Animal):
def __init__(self, name, age, color):
super().__init__(name, age) # Inheritance: Inheriting from Animal class
self.color = color

def speak(self): # Polymorphism: Method overriding


return f"{self.name} says Meow!"
# Creating objects of the classes
dog = Dog("Buddy", 3, "Golden Retriever")
cat = Cat("Whiskers", 2, "White")

# Accessing methods and attributes


print(dog.speak()) # Polymorphism: Dog class overrides speak method
print(cat.speak()) # Polymorphism: Cat class overrides speak method
print(f"{dog.name} is {dog.age} years old and is a {dog.breed}.") # Encapsulation: Accessing
private attribute

```

### **Explanation of the Program:**

- **Encapsulation**: The attributes `name` and `age` in the `Animal` class are encapsulated,
meaning they are contained within the class and can be accessed or modified only via methods.

- **Inheritance**: The `Dog` and `Cat` classes inherit from the `Animal` class, allowing them to
reuse the attributes (`name`, `age`) and methods of the `Animal` class.

- **Polymorphism**: Both `Dog` and `Cat` classes override the `speak()` method of the parent
`Animal` class. When `speak()` is called on instances of these classes, they behave differently,
displaying "Woof!" and "Meow!", respectively.

---

### Output of the program:


```
Buddy says Woof!
Whiskers says Meow!
Buddy is 3 years old and is a Golden Retriever.
```

This program demonstrates how encapsulation, inheritance, and polymorphism are


implemented in Object-Oriented Programming.

You might also like