Python Programs for Oops
Python Programs for Oops
```python
# Define a class
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}, Grade: {self.grade}")
### 2. **Encapsulation**
```python
class Employee:
def __init__(self, name, salary):
self.__name = name # Private attribute
self.__salary = salary # Private attribute
def get_info(self):
return f"Employee: {self.__name}, Salary: {self.__salary}"
### 3. **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
class Dog(Animal):
def speak(self):
print(f"{self.name} barks.")
# Child class
class Cat(Animal):
def speak(self):
print(f"{self.name} meows.")
# Create instances
dog = Dog("Buddy")
cat = Cat("Kitty")
dog.speak()
cat.speak()
```
### 4. **Polymorphism**
```python
class Bird:
def sound(self):
print("Birds make sound")
class Sparrow(Bird):
def sound(self):
print("Sparrow chirps")
class Pigeon(Bird):
def sound(self):
print("Pigeon coos")
def make_sound(bird):
bird.sound()
sparrow = Sparrow()
pigeon = Pigeon()
make_sound(sparrow)
make_sound(pigeon)
```
```python
from abc import ABC, abstractmethod
# Abstract class
class Shape(ABC):
@abstractmethod
def area(self):
pass
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
# Create objects
rect = Rectangle(5, 3)
circle = Circle(7)
```python
class MathOperations:
def add(self, a=None, b=None, c=None):
if a is not None and b is not None and c is not None:
return a + b + c
elif a is not None and b is not None:
return a + b
else:
return "Insufficient arguments"
# Create an object
math_op = MathOperations()
print(math_op.add(10, 20))
print(math_op.add(10, 20, 30))
print(math_op.add(10))
```
```python
class Parent:
def show(self):
print("This is the parent class.")
class Child(Parent):
def show(self):
print("This is the child class overriding the parent class.")
# Create objects
parent_obj = Parent()
child_obj = Child()
parent_obj.show()
child_obj.show()
```
```python
class Person:
def __init__(self, name=None, age=None):
if name is not None and age is not None:
print(f"Name: {name}, Age: {age}")
elif name is not None:
print(f"Name: {name}")
else:
print("No information provided")
```python
class Father:
def skill(self):
print("Father is good at painting.")
class Mother:
def skill(self):
print("Mother is good at singing.")
class Child(Father, Mother):
def skill(self):
super().skill() # Calls skill method from Father due to method resolution
order
# Create object
child = Child()
child.skill()
```
```python
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# Overloading + operator
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Point({self.x}, {self.y})"
# Create objects
p1 = Point(2, 3)
p2 = Point(4, 5)
# Add objects
p3 = p1 + p2
print(p3)
```