Ads

Ads

What is OOPs and explain with example

 What is OOPs and explain with example

OOP stands for Object-Oriented Programming. It's a programming paradigm based on the concept of "objects," which can contain data and code to manipulate that data.

For example, let's say we're building a program to model different shapes. We could create a class called Shape that represents common properties and behaviors of shapes, such as area() and perimeter(). Then, we can create subclasses like Rectangle, Circle, and Triangle that inherit from Shape and provide specific implementations for those shapes' properties and behaviors.

A simplified example in Python:

 

python
class Shape:
def area(self): pass def perimeter(self): pass class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height def perimeter(self): return 2 * (self.width + self.height) class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius ** 2 def perimeter(self): return 2 * 3.14 * self.radius # Example usage: rect = Rectangle(5, 4) print("Rectangle Area:", rect.area()) print("Rectangle Perimeter:", rect.perimeter()) circle = Circle(3) print("Circle Area:", circle.area()) print("Circle Perimeter:", circle.perimeter())



In this example, Shape is the base class, and Rectangle and Circle are subclasses. They inherit common methods like area() and perimeter() from Shape but provide their own implementations. This allows for code reuse and organization based on the concept of objects and their relationships.


A simple example of OOP in Python:

 

python

class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): return f"{self.year} {self.make} {self.model}" def read_odometer(self): print(f"This car has {self.odometer_reading} miles on it.") def update_odometer(self, mileage): if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self, miles): self.odometer_reading += miles # Example usage: my_car = Car("Toyota", "Corolla", 2022) print(my_car.get_descriptive_name()) my_car.read_odometer() my_car.update_odometer(100) my_car.read_odometer() my_car.increment_odometer(50) my_car.read_odometer()



In this example, Car is a class representing cars. Each instance of Car has attributes like make, model, year, and odometer_reading. The methods like get_descriptive_name(), read_odometer(), update_odometer(), and increment_odometer() define behaviors related to cars. This approach organizes the code around the concept of a car, making it easier to work with and maintain.

Another example using Python to simulate a simple banking system:

 

python

class BankAccount: def __init__(self, account_number, owner, balance=0): self.account_number = account_number self.owner = owner self.balance = balance def deposit(self, amount): self.balance += amount print(f"Deposited {amount} into account {self.account_number}. New balance: {self.balance}") def withdraw(self, amount): if self.balance >= amount: self.balance -= amount print(f"Withdrew {amount} from account {self.account_number}. New balance: {self.balance}") else: print("Insufficient funds") def display_balance(self): print(f"Account {self.account_number} belongs to {self.owner}. Current balance: {self.balance}") # Example usage: account1 = BankAccount("123456", "John Doe") account1.display_balance() account1.deposit(1000) account1.withdraw(500) account1.display_balance() account2 = BankAccount("789012", "Jane Smith", 2000) account2.display_balance() account2.withdraw(2500)


In this example, BankAccount is a class representing bank accounts. Each account has attributes such as account_number, owner, and balance. Methods like deposit(), withdraw(), and display_balance() define actions that can be performed on bank accounts. This allows us to create multiple instances of BankAccount, each representing a different bank account with its own balance and owner.raw(), and display_balance() define actions that can be performed on bank accounts. This allows us to create multiple instances of BankAccount, each representing a different bank account with its own balance and owner.


#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!